0

I am trying to get more proficient with Java. I am making a simple card game to test my basic skills. I have come across a problem in my Deck class. The for-loops are not creating new card objects in my arraylist even though they should. When I call getTotalCards the size of the arraylist is always 0. Any ideas of what I am doing wrong?

Main Class:

package gameofcards;

import java.util.Random;
import static gameofcards.Card.*;

public class GameOfCards {

public static void main(String[] args) {
    Deck d1 = new Deck();
   System.out.println(d1.getTotalCards());

    }
}

Deck Class:

package gameofcards;

import java.util.ArrayList;

public class Deck {
private ArrayList<Card> cards;

public Deck(){
    cards = new ArrayList<Card>();

    for(int i = 1; i >=4; i++){
        for(int j = 1; j >=13; j++){
            cards.add(new Card(i,j));
        }
    }

}

public int getTotalCards(){
    return cards.size();
}

}

Card Class:

package gameofcards;


public class Card {

private int Suite;
private int Rank;

public static final int Club = 1;
public static final int Diamond = 2;
public static final int Hearts = 3;
public static final int Spade = 4;

public static final int Jack = 10;
public static final int Queen = 11;
public static final int King = 12;
public static final int Ace = 13;

public void setSuite(int cardSuite){
    Suite=cardSuite;
}

public int getSuite(){
    return Suite;
}

public void setRank(int cardRank){
    Rank=cardRank;
}

public int getRank(){
    return Rank;
}

public Card(int Suite, int Rank){
    this.Suite = Suite;
    this.Rank = Rank;
}

public String cardSuite(){
    switch(Suite){
        case Club: return "Clubs";
        case Diamond: return "Diamonds";
        case Hearts: return "Hearts";
        case Spade: return "Spades";
        default: return "Joker";
    }
}

public String cardRank() {
    switch(Rank){
        case 2: return "2";
        case 3: return "3";
        case 4: return "4";
        case 5: return "5";
        case 6: return "6";
        case 7: return "7";
        case 8: return "8";
        case 9: return "9";
        case 10: return "Jack";
        case 11: return "Queen";
        case 12: return "King";
        case 13: return "Ace";
        default: return "Joker";
    }
}

}
0

2 Answers 2

2

Your loop conditions are wrong.

Instead of:

for(int i = 1; i >=4; i++){

It should be:

for(int i = 1; i <=4; i++){

And a similar problem with the inner loop condition.

You want to loop while i is less than or equal to 4.

It wasn't adding cards because it never entered the loop in the first place.

An easy way to have figured this out would have been to put a println inside the loop and see what happens. You would have noticed that it never printed.

Sign up to request clarification or add additional context in comments.

Comments

2

The conditions in for loop are wrong. should be i<=4 and j<=13

for(int i = 1; i <=4; i++){
    for(int j = 1; j <=13; j++){
        cards.add(new Card(i,j));
    }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.