A.Lopez
A.Lopez

Reputation: 25

Why do my variables all disappear in this method?

I'm trying to create a deck of cards with the CardController class.

The Card class (unseen) represents individual cards and comes with the constructor public Card(String cardValue, CardType cardType).

My deck of cards is filled out correctly until my deckIndexTracker hits 10, at which point the code moves to a switch statement to fill out the card values

Jack, Queen, King and Ace.

After using Eclipse's debugging tool, I find that every single one of my variables vanish after the method goes past deckIndexTracker 9.

What could be going on here?

public class CardController {
    public static Card[] makeDeck() {//2 to 10 + J,Q,K,A
        Card[] deck = new Card[51];
        int deckIndexTracker = 1;
        while(deckIndexTracker <= 13) {
            if(deckIndexTracker <= 9) {
            deck[deckIndexTracker - 1] = new Card(String.valueOf(deckIndexTracker + 1), CardType.HEART);
            deckIndexTracker++;             
            }else {
                switch(deckIndexTracker) {
                case 10:
                    deck[deckIndexTracker - 1] = new Card("Jack", CardType.HEART);
                    break;
                case 11:
                    deck[deckIndexTracker - 1] = new Card("Queen", CardType.HEART);
                    break;
                case 12:
                    deck[deckIndexTracker - 1] = new Card("King", CardType.HEART);
                    break;
                case 13:
                    deck[deckIndexTracker - 1] = new Card("Ace", CardType.HEART);
                }
            }
        }

Upvotes: 0

Views: 127

Answers (5)

Nexevis
Nexevis

Reputation: 4667

In my opinion you are going about this the wrong way to begin with. Instead of using a switch statement when constructing the deck, it would make more sense to use the switch in the constructor for the Card. Card can then handle different cases inside of the constructor easily which will make your code a lot more reusable.

The Card class would look like this:

private String cardVal;
private CardType suit;

public Card(int value, CardType suit){

    switch(value)
    {
        case 1:
        case 14:
            this.cardVal = "Ace";
            break;
        case 11:
            this.cardVal = "Jack";
            break;
        case 12:
            this.cardVal = "Queen";
            break;
        case 13:
            this.cardVal = "King";
            break;
        default: //Default case that handles nonface cards
            this.cardVal = String.valueOf(value);
    }
    this.suit = suit;
}

Then look how easily you can build the entire deck with every suit:

    ArrayList<Card> deck = new ArrayList<>();

    for (int deckIndexTracker = 2; deckIndexTracker <= 14; deckIndexTracker++)
    {
        deck.add(new Card(deckIndexTracker, CardType.HEART));
        deck.add(new Card(deckIndexTracker, CardType.CLUB));
        deck.add(new Card(deckIndexTracker, CardType.SPADE));
        deck.add(new Card(deckIndexTracker, CardType.DIAMOND));
    }

Use constructors to your advantage! I even put a case where Ace is used for both 1 and 14.

Upvotes: 0

KunLun
KunLun

Reputation: 3225

Your while loop looks like is an infinite loop, because you stop increasing deckIndexTracker value after 10.

You can make your code more simple. Using just switch:

int deckIndexTracker = 2; 
//start from 2, 
//because the case where value is equal with 9
//you add new Card("9 + 1",...) which should be "Jack" in my opinion.
//start from 2 -> add value of index in Card, but remove 2 at deck index.

while(deckIndexTracker <= 13) {

    switch(deckIndexTracker) {

        case 10:
            deck[deckIndexTracker - 2] = new Card("Jack", CardType.HEART);
            break;

        case 11:
            deck[deckIndexTracker - 2] = new Card("Queen", CardType.HEART);
            break;

        case 12:
            deck[deckIndexTracker - 2] = new Card("King", CardType.HEART);
            break;

        case 13:
            deck[deckIndexTracker - 2] = new Card("Ace", CardType.HEART);
            break;

        default:
            deck[deckIndexTracker - 2] = new Card(String.valueOf(deckIndexTracker), CardType.HEART);
            break;

    }

    deckIndexTracker++;

}

If you use deckIndexTracker only for this part of your code, I recommend to use for loop:

for(int deckIndexTracker = 2; deckIndexTracker <= 13; deckIndexTracker++){

    switch(deckIndexTracker){

        //same like in while

    }

}

Upvotes: 1

Pratik Pawar
Pratik Pawar

Reputation: 91

The problem with above code is the position of #deckIndexTracker++;. It is in if block. Upto deckIndexTracker=9, it will work properly. Once deckIndexTracker=10 it will come to else block and there deckIndexTracker count will not be increased and it will remain 10. Due to which it will become infinite loop. Solution : Place #deckIndexTracker++; after if-else block

Upvotes: 0

Santosh
Santosh

Reputation: 886

You are incrementing the value of deckIndexTracker in if() so once control goes to else part, the variable is not increased.

Add

deckIndexTracker++;

after if-else in while loop.

Upvotes: 0

Jarett LeVan
Jarett LeVan

Reputation: 389

You need to add deckIndexTracker++ at the end of your else statement so it continues on and doesn't stop at the Jack. Or, you could add it at the end of the while loop and remove the one that is inside your if statement.

Upvotes: 0

Related Questions