-2

I seem to be getting an error with my deck of cards project. I'm trying to print out a shuffled deck of cards and I've had help already, but this error is now stopping me from progressing

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 51
 at Pack.<init>(Pack.java:14)
 at PackTester.main(PackTester.java:14)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)
  public class Pack {

private PlayingCard[] deck;   // An array of 52 cards, representing the deck.
private int cardsUsed; // How many cards have been dealt from the deck.

/**
* Creating an unshuffled deck of cards
*/
public Pack() {
   deck = new PlayingCard[51]; //Creates an array of 52 playing cards
   int cardCt = 0; // How many cards have been created so far.
   for ( int suit = 0; suit <= 3; suit++ ) { //If a suit is complete, move to the next suit
      for ( int rank = 1; rank <= 14; rank++ ) { //Builds a complete suit
         deck[51] = new PlayingCard(rank, suit);
         cardCt++; //Adds one to the card count
      }
   }
   cardCt = 0;
}

/**
* Shuffling a deck of cards
*/
public void shuffle() {
      // Put all the used cards back into the deck, and shuffle it into
      // a random order.
    for ( int i = 51; i > 0; i-- ) { 
        int rand = (int)(Math.random()*(i+1));
        PlayingCard temp = deck[i];
        deck[i] = deck[rand];
        deck[rand] = temp;
    }
    cardsUsed = 0;
}

public @Override String toString() {

String deckStr = "";

for (int i=0; i<52; i++) {
    deckStr = deckStr + deck[i].toString() + " ";
}

return deckStr;
}
} // end class Pack

And here is the tester class.

public class PackTester {

public static void main(String[] args)
{
    Pack myPack = new Pack();
    myPack.shuffle();
    System.out.println(myPack.toString());
}
}

I just don't know where to go from here so any help would be appreciated.

Ben Parry
  • 133
  • 1
  • 4
  • 19
  • Consider using Lists and have your PlayingCard class implement the Comparable interface, Then you can easily shuffle the deck by calling Collections.shuffle(myCardList) – Johan Prins Feb 23 '15 at 11:54

2 Answers2

0

In order to create a Deck of 52 cards you need :

deck = new PlayingCard[52];

In addition, it makes little sense for your loop to always assign a card to the 51st position :

deck[51] = new PlayingCard(rank, suit);

This would make more sense :

public Pack() {
   deck = new PlayingCard[52]; //Creates an array of 52 playing cards
   int cardCt = 0; // How many cards have been created so far.
   for ( int suit = 0; suit <= 3; suit++ ) { //If a suit is complete, move to the next suit
      for ( int rank = 1; rank < 14; rank++ ) { //Builds a complete suit
         deck[cardCt] = new PlayingCard(rank, suit);
         cardCt++; //Adds one to the card count
      }
   }
}

Also note that there should be 13 ranks, not 14.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Still receiving the same error – Ben Parry Feb 23 '15 at 10:55
  • @BenParry You can't be getting the `java.lang.ArrayIndexOutOfBoundsException: 51` error if you changed the length of your array to 52. Either you didn't change it, or you are getting a different error. – Eran Feb 23 '15 at 10:57
  • It is though, I've changed 51 to 52, I'm getting the same error, but the number is 52 now – Ben Parry Feb 23 '15 at 11:01
  • @BenParry Did you change the condition of the inner loop to `rank < 14`? If not, you are creating too many cards, and there's not enough room for them in your array. – Eran Feb 23 '15 at 11:03
  • Thanks for the help, just had another problem, but it's been resolved now and the program works – Ben Parry Feb 23 '15 at 11:07
0

You defined array as:

deck = new PlayingCard[51];

And you are trying to add element like:

deck[51] = new PlayingCard(rank, suit);

You are trying to set 51st index element in an array.

Also in shuffle method:

for ( int i = 51; i > 0; i-- ) { //either start with 50 or define array as PlayingCard[52]
int rand = (int)(Math.random()*(i+1));
 PlayingCard temp = deck[i];

Remember index of an array starts from 0 and goes up til n -1, so you need 52nd element (52 cards, i.e. 52nd would be accessed by index 51), increase the array capacity from 51 to 52 when you define the array.

SMA
  • 36,381
  • 8
  • 49
  • 73