Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Mernak

macrumors 6502
Original poster
Apr 9, 2006
435
16
Kirkland, WA
Okay I promised myself that I wouldn't do this, but neither I nor my teacher can figure this out.

Code:
class CardDeck
{
...
public void initializeDeck()
{
     ArrayList<String> temp1 = new ArrayList<String>(52);
     ArrayList<Integer> temp2 = new ArrayList<Integer>(52);
     int j=0; //j is rank
     int k=0; //s is suit
     String tempCard;
        
     while ( true )
     {
         tempCard = rank[j] + "of" + suit[k];
         temp1.add(tempCard);
         k++;
         if (k==4) { j++ ; k=0; }
         if (j==13 && k==0) break;
     }
     for (int i=0 ; i<52 ; i++)
     {
         temp2.add(0);
     }
     setCards(temp1);
     setCardCount(temp2); //same error as setCards()
}
...
public void setCards(ArrayList<String> temp)
{
     currentGameCards = temp;
     //debug, printing here works
     for (int i=0 ; i<52 ; i++)
     {
         System.out.printf("\ngetCards: %d : %s", i, currentGameCards.get(i) );
     }
}
...
public final String[] suit =
      {"Hearts","Diamonds","Spades","Clubs"};
    public final String[] rank =
      {"Ace", "2", "3", "4", "5", "6" , "7" , "8" , "9" , "10" , "Jack" , "Queen" , "King"};
public ArrayList<String> currentGameCards;// = new ArrayList<String>(52)same error
public ArrayList<Integer> currentGameCardsCount;// = new ArrayList<Integer>(52) same error
But when I try to get anything from the ArrayList using this in another method, it returns an error saying "Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0"
Code:
for (int i=0 ; i<52 ; i++)
     {
         System.out.printf("\n%d : %s : %d", i, currentGameCards.get(i),currentGameCardsCount.get(i) );
     }
 

psingh01

macrumors 68000
Apr 19, 2004
1,586
629
I just copied your code and ran it and didn't get an error. Perhaps there is something not set correctly in your IDE?

The output I got:
Code:
getCards: 0 : AceofHearts
getCards: 1 : AceofDiamonds
getCards: 2 : AceofSpades
getCards: 3 : AceofClubs
getCards: 4 : 2ofHearts
getCards: 5 : 2ofDiamonds
getCards: 6 : 2ofSpades
getCards: 7 : 2ofClubs
getCards: 8 : 3ofHearts
getCards: 9 : 3ofDiamonds
getCards: 10 : 3ofSpades
getCards: 11 : 3ofClubs
getCards: 12 : 4ofHearts
getCards: 13 : 4ofDiamonds
getCards: 14 : 4ofSpades
getCards: 15 : 4ofClubs
getCards: 16 : 5ofHearts
getCards: 17 : 5ofDiamonds
getCards: 18 : 5ofSpades
getCards: 19 : 5ofClubs
getCards: 20 : 6ofHearts
getCards: 21 : 6ofDiamonds
getCards: 22 : 6ofSpades
getCards: 23 : 6ofClubs
getCards: 24 : 7ofHearts
getCards: 25 : 7ofDiamonds
getCards: 26 : 7ofSpades
getCards: 27 : 7ofClubs
getCards: 28 : 8ofHearts
getCards: 29 : 8ofDiamonds
getCards: 30 : 8ofSpades
getCards: 31 : 8ofClubs
getCards: 32 : 9ofHearts
getCards: 33 : 9ofDiamonds
getCards: 34 : 9ofSpades
getCards: 35 : 9ofClubs
getCards: 36 : 10ofHearts
getCards: 37 : 10ofDiamonds
getCards: 38 : 10ofSpades
getCards: 39 : 10ofClubs
getCards: 40 : JackofHearts
getCards: 41 : JackofDiamonds
getCards: 42 : JackofSpades
getCards: 43 : JackofClubs
getCards: 44 : QueenofHearts
getCards: 45 : QueenofDiamonds
getCards: 46 : QueenofSpades
getCards: 47 : QueenofClubs
getCards: 48 : KingofHearts
getCards: 49 : KingofDiamonds
getCards: 50 : KingofSpades
getCards: 51 : KingofClubs
 

jeremy.king

macrumors 603
Jul 23, 2002
5,479
1
Holly Springs, NC
Code:
import java.util.ArrayList;

class CardDeck {
  private final String[] suit = { "Hearts", "Diamonds", "Spades", "Clubs" };
  private final String[] rank = { "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King" };
  public ArrayList<String> currentGameCards;
  public ArrayList<Integer> currentGameCardsCount;

  public void initializeDeck() {
    ArrayList<String> temp1 = new ArrayList<String>(52);
    ArrayList<Integer> temp2 = new ArrayList<Integer>(52);
    int j = 0; // j is rank
    int k = 0; // s is suit
    String tempCard;

    while (true) {
      tempCard = rank[j] + "of" + suit[k];
      temp1.add(tempCard);
      k++;
      if (k == 4) {
        j++;
        k = 0;
      }
      if (j == 13 && k == 0)
        break;
    }
    for (int i = 0; i < 52; i++) {
      temp2.add(0);
    }
    setCards(temp1);
  }

  public void setCards(ArrayList<String> temp) {
    currentGameCards = temp;
    // debug, printing here works
//    for (int i = 0; i < 52; i++) {
//      System.out.printf("getCards: %d : %s \n", i, currentGameCards.get(i));
//    }
  }

  public static void main(String[] args) {
    CardDeck deck = new CardDeck();
    deck.initializeDeck();
    for (int i=0 ; i<52 ; i++)
    {
        System.out.printf("\n%d : %s", i, deck.currentGameCards.get(i) );
    }

  }
}

Works fine here.

I'll refrain from ripping this apart, but keep in mind this could be implemented better (hint: consider a Card class and then deck would just be a list of Cards, and no need for all the temp arrays, oh and encapsulation never hurt anyone ;) )
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
<snip>
Works fine here.

I'll refrain from ripping this apart, but keep in mind this could be implemented better (hint: consider a Card class and then deck would just be a list of Cards, and no need for all the temp arrays, oh and encapsulation never hurt anyone ;) )

While we're taking (good natured) pot-shots, so rarely is while(true) the right answer to any question. That looks like a perfectly fine scenario to use a nested for loop.

Also, did the instructor request that you use printf? The java print/println syntax is so much clearer, in my opinion.

-Lee
 

Mernak

macrumors 6502
Original poster
Apr 9, 2006
435
16
Kirkland, WA
Yeah, I know my code is really messy/inefficient, it is from it being for something completely different and then morphing into what it is now. Looks like I was missing the class name before calling the variable, which is what caused the problems (thanks kingjr3).

p.s. I knew there was a better way to do that loop, I just couldn't figure it out. And I just like printf, my personal preference.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.