cress
cress

Reputation: 409

Deck of card program returning null cards

I'm trying to build a deck of cards and print each one out. I have a class for a single Card and a class that holds a Deck. I try to print all the card ranks and suits with a toString() method in my Card class. For some reason, it only prints 13 cards (2-A all CLUBS) and the rest are null. Here's the code:

Card.java

public class Card
{
    private enum Suit {HEARTS, DIAMONDS, SPADES, CLUBS};
    private Suit suit;
    private String rank;

    public Card(String r, int index)
    {
            rank = r;
            setSuit(index);
    }

    public void setSuit(int index)
    {
            switch(index)
            {
                  case 0:
                     suit = suit.HEARTS;
                     break;
                  case 1:
                     suit = suit.DIAMONDS;
                     break;
                  case 2:
                     suit = suit.SPADES;
                     break;
                  case 3:
                     suit = suit.CLUBS;
                     break;
            }
    }

    public String toString()
    {
            return (rank + " of " + suit);
    }
}

Deck.java

public class Deck
{
    private Card [] card = new Card[52];

    public Deck()
    {
            for(int i = 0; i < 13; i++)
            {
                  for(int j = 0; j < 4; j++)
                  {
                     if(i < 9)
                         card[i] = new Card(Integer.toString(2+i), j);
                     else if(i == 9)
                         card[i] = new Card("J", j);
                     else if(i == 10)
                         card[i] = new Card("Q", j);
                     else if(i == 11)
                         card[i] = new Card("K", j);
                     else
                         card[i] = new Card("A", j);
                  }
            }
    }

    public Card getInfo(int index)
    {
            return card[index];
    }
}

Test.java

public class Test
{
    public static void main(String [] args)
    {
            Deck deck = new Deck();

            for(int i = 0; i < 52; i++)
                     System.out.println(deck.getInfo(i));
    }
}

Any help would be appreciated!

EDIT: I can't use anything you don't see here.

Upvotes: 0

Views: 268

Answers (5)

sunnysidedown916
sunnysidedown916

Reputation: 175

one recommendation: Have a default case in your switch statement

Your error would be this here, your card index only is set from 0 to 12 so all your cards will be initlized only from card 0 to card 12.

Change this: card[i] = .... to this: card[i + 13*j] = ....

Upvotes: 0

Kevin L
Kevin L

Reputation: 1066

The problem here is that you are overwriting card[i] 3 times. When you loop through j, you write to the same card[i], so since clubs are the last value, they are the only ones in the deck (all others being overwritten)

Upvotes: 0

Mureinik
Mureinik

Reputation: 311198

You have two loops in Deck() - the i loop goes over face values and the j loop goes over suites. However, you always assign to card[i], effectively overwriting it four times, one for each suite. Instead, you should take i into account, and assign the value to card[i * 4 + j]:

public Deck()
{
        for(int i = 0; i < 13; i++)
        {
              for(int j = 0; j < 4; j++)
              {
                 index = (i * 4) + j;
                 if(i < 9)
                     card[index] = new Card(Integer.toString(2+i), j);
                 else if(i == 9)
                     card[index] = new Card("J", j);
                 else if(i == 10)
                     card[index] = new Card("Q", j);
                 else if(i == 11)
                     card[index] = new Card("K", j);
                 else
                     card[index] = new Card("A", j);
              }
        }
}

Upvotes: 1

Tom McGee
Tom McGee

Reputation: 136

You're just setting the same element of your array to 4 cards each iteration of the loop you need i to equal 51 at some point to set all the cards or use a counter of some sort.

Upvotes: 0

Kakarot
Kakarot

Reputation: 4252

In your Deck constructor you are never assigning Cards for index greater than 12 (Since i is never greater than 12)

Instead of using card[i] , you should use card[(i+1)*j]

Other option would be to use another variable(say index) to keep track of the card count.

 public Deck()
{
        int index = 0;
        for(int i = 0; i < 13; i++)
        {
              for(int j = 0; j < 4; j++)
              {
                 if(i < 9)
                     card[index] = new Card(Integer.toString(2+i), j);
                 else if(i == 9)
                     card[index] = new Card("J", j);
                 else if(i == 10)
                     card[index] = new Card("Q", j);
                 else if(i == 11)
                     card[index] = new Card("K", j);
                 else
                     card[index] = new Card("A", j);

               // Now increment the index
               index++;
              }
        }
}

Upvotes: 1

Related Questions