title: A Deck of Cards
body: <p>I'm a fairly noobish guy trying to learn Java, and I'm having a little trouble completing a task I gave myself. Basicaly I'm trying to do the exercises at the end of this <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/QandE/creating-questions.html" rel="nofollow">page.</a></p>
  
  <p>I managed to finish it with three classes. <strong>The Card</strong>:</p>
  
  <pre><code>public class Card {
  
  public int nRank; // Used later
  public int maxRank = 13; //The max number of Ranks
  public int nSuit; // Used later
  public int maxSuit = 4; // Max number of suits
  
  
  //Associate both rank and suit numbers with strings
  
  public String[] ranks = new String[maxRank - 1];
  {
      ranks[0] = "two";
      ranks[1] = "three";
      ranks[2] = "four";
      ranks[3] = "five";
      ranks[4] = "six";
      ranks[5] = "seven";
      ranks[6] = "eight";
      ranks[7] = "nine";
      ranks[8] = "ten";
      ranks[9] = "Jack";
      ranks[10] = "Queen";
      ranks[11] = "King";
      ranks[12] = "Ace";
  
      }
  
  public String[] suits = new String[maxSuit - 1];
  {
      suits[0] = "Clubs";
      suits[1] = "Diamonds";
      suits[2] = "Spades";
      suits[3] = "Hearts";
      }
  
  public String suit = suits[nSuit]; //The suit string of the card whose suit number is nSuit
  public String rank = ranks[nRank]; //Same but with ranks
  
  //Constructor for the Card object, with two arguments, x for rank, y for suit
  public Card(int x,int y){
      this.nRank = x;
      this.nSuit = y;
  }
  
  //method to get which card it is in a string
  public String whatCard(){
      return rank + " of " + suit;
  }
  }
  </code></pre>
  
  <p><strong>The Deck:</strong></p>
  
  <pre><code>    public class Deck {
  
  public static int nRanks = 13; //number of ranks
  public static int nSuits = 4; // number of suits
  public static int nCard = nRanks * nSuits; // number of cards
  
  Card[] deck = new Card[nCard -1]; //new array called deck to store all the cards 
  int h = 0; //a variable to control the place of each card in the array
  //constructor for the Deck
  public Deck() {
  
  while(h &lt; 52){ // loop until there are 52 cards
  
  // cycles through all the possible combinations between i(ranks) and j(suits) and creates a card with each
  for(int i = 1; i &lt;= nRanks; i++){
  
      for(int j = 1; j &lt;= nSuits; j++){
  
          deck[h] = new Card(i,j); // creation of the card
          h++; // adds 1 to to h so the program knows how many cards are there
          }
      }
  
  }
  }
  //method for getting a card depending on its position in the array(x)
      public Card getCard(int x){
      return deck[x-1];   
  }
  }
  </code></pre>
  
  <p>And the displayer of the cards/deck, which I called <strong>Shuffle</strong>:</p>
  
  <pre><code>public class Shuffle {
  
  public static void main(String[] args){
          Deck newDeck = new Deck(); // creates a new Deck object
          //loops through all the cards in the deck
      for(int i = 0; i &lt; Deck.nCard; i++){
          System.out.println(newDeck.getCard(i).whatCard()); // prints each card
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Although eclipse doesn't notice any errors in the code, when I try to compile, I'm shown this:</p>
  
  <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
      at Card.&lt;init&gt;(Card.java:26)
      at Deck.&lt;init&gt;(Deck.java:20)
      at Shuffle.main(Shuffle.java:5)
  </code></pre>
  
  <p>What have I missed?</p>