Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Card Deck project not working

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Card Deck project not working

    Ok, so I have tried to create a deck of cards using 2 classes, Deck.java and Card.java
    But every time I try to add a card to the deck ArrayList, using a for statement, the deck ends up containing only 1 type of card, multiple times. This occurs no matter the statement I try to use. The for statement that is commented out is the one i first tried, and I googled some stuff and tried the non commented one. The statement is located in the Deck constructor.

    Deck.java
    import java.util.ArrayList;
     
    @SuppressWarnings({ "unchecked", "rawtypes", "static-access" })
    public class Deck {
     
    	public static ArrayList<String> deckName = new ArrayList();
    	public static ArrayList<Card> deck = new ArrayList();
     
    	public Deck(){
    		/*for(int suit = 1; suit < 5; suit++){
    			for(int id = 1; id < 15; id++){
    				//System.out.println( new Card(id, suit).toString() );
    				deckName.add( new Card(id, suit).toString() );
    				deck.add( new Card(id, suit) );
    			}
    		}*/
    		Card a;
    		for(int i = 1; i < 14; i++){ // get int 1 through 13
    			a = new Card(i, 1); // new Card w/ id=i & suit = 1
    			System.out.println( a.toString() + "~");
    			deck.add(a); //add new Card to deck
     
    		}
    		System.out.println( deck.size() );
     
     
    	}
     
    	public static void main(String args[]){
    		new Deck();
    		for(int i = 0; i < Deck.deckName.size(); i++){
    			System.out.println(Deck.deckName.get(i));
    		}
    		for(int i = 0; i < Deck.deck.size(); i++){// deck[i] = card
    			System.out.println(Deck.deck.get(i).name);
    		}
    		//System.out.println( Deck.deck.get(1).toString() );
     
    	}
    }
    Card.java
     
    @SuppressWarnings("static-access")
    public class Card {
     
    	public static int id;
    	public static String suit, name;
     
    	public Card(int id, int suitNum){
    		this.id = id;
     
    		if(suitNum == 1){
    			suit = "Diamonds";
    		}else if(suitNum == 2){
    			suit = "Hearts";
    		}else if(suitNum == 3){
    			suit = "Spades";
    		}else if(suitNum == 4){
    			suit = "Clubs";
    		}else{
    			suit = "UNKNOWN";
    		}
     
    		if(id == 11){
    			name = "Jack" + " of " + suit;
    		}else if(id == 12){
    			name = "Queen" + " of " + suit;
    		}else if(id == 13){
    			name = "King" + " of " + suit;
    		}else if(id == 14){
    			name = "Ace" + " of " + suit;
    		}else{
    			name = id + " of " + suit;
    		}
     
     
    	}
     
    	@Override
    	public String toString(){
    		return name;
    	}
     
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Card Deck project not working

    Suggested reading: Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    Any reason you are suppessing all the compiler warnings?

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Card Deck project not working

    Quote Originally Posted by copeg View Post
    only for those yellow warning lines that dont affect the code. I can take that out and see what happens. Ill post the results.
    I am also going to try to do this at the loop:
    Card a = new Card( 0, );
    		for(int i = 2; i < 15; i++){ // get int 1 through 13
    			//a = new Card(i, 1); // new Card w/ id=i & suit = 1
    			a.setID(i);
    			System.out.println( a.toString() + "~");
    			deck.add(a); //add new Card to deck
     
    		}

  4. #4
    Member
    Join Date
    May 2013
    Posts
    33
    Thanks
    0
    Thanked 9 Times in 9 Posts

    Default Re: Card Deck project not working

    If you read through the link Copeg supplied it answers the question. The problem isn't the for loop, it is a problem with your variables.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Card Deck project not working

    only for those yellow warning lines that dont affect the code
    But they do affect the code - there is a reason the compiler issues these warnings

  6. #6
    Junior Member
    Join Date
    May 2013
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Card Deck project not working

    Quote Originally Posted by theoriginalanomaly View Post
    If you read through the link Copeg supplied it answers the question. The problem isn't the for loop, it is a problem with your variables.
    Yea, I figured out that when I add the Card to the ArrayList, when I update the properties, all the properties are updated. How do I make it so that it will add a new Card every time the loop goes through.
    For some reason ".add( new Card(id, suit) );" wasn't working.

    --- Update ---

    Quote Originally Posted by copeg View Post
    But they do affect the code - there is a reason the compiler issues these warnings
    Ok, I took out the suppressing ( the only warnings it was giving me were that the array lists didn't contain any values when initialized). I have turned the loop to look like the following:
    Card a = new Card( 0, 0 );
    for(int i = 2; i < 15; i++){ // get int 1 through 13
    //a = new Card(i, 1); // new Card w/ id=i & suit = 1
    a.setID(i);
    System.out.println( a.toString() + "~");
    deck.add( new Card( i, 0) ); //add new Card to deck
    }
    System.out.println( deck.size() );
    It is supposed to add a new Card to the array in each loop of the for statement, but is still resetting all other Card values.
    Result:
    2 of UNKNOWN~
    3 of UNKNOWN~
    4 of UNKNOWN~
    5 of UNKNOWN~
    6 of UNKNOWN~
    7 of UNKNOWN~
    8 of UNKNOWN~
    9 of UNKNOWN~
    10 of UNKNOWN~
    Jack of UNKNOWN~
    Queen of UNKNOWN~
    King of UNKNOWN~
    Ace of UNKNOWN~
    13
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN


    --- Update ---

    So I went through and put in some debugging output code and this is what the loop looks like:
    Card a = new Card( 0, 0 );
    		System.out.println( "~start #1~");
    		for(int i = 2; i < 15; i++){ // get int 1 through 13
    			//a = new Card(i, 1); // new Card w/ id=i & suit = 1
    			a.setID(i);
    			System.out.println( (new Card(i, 0)).toString() + "~");
    			deck.add( new Card( i, 0) ); //add new Card to deck
    			System.out.println( "~~start #2~~");
    			for(int j = 0; j < deck.size(); j++){				
    				System.out.println(
    						deck.get(j).toString()
    					);
    			}
    			System.out.println( "~~end #2~~");
    		}
    		System.out.println( "~end #1~" );
    		System.out.println( "size: " + deck.size() );
    And the result:
    ~start #1~
    2 of UNKNOWN~
    ~~start #2~~
    2 of UNKNOWN
    ~~end #2~~
    3 of UNKNOWN~
    ~~start #2~~
    3 of UNKNOWN
    3 of UNKNOWN
    ~~end #2~~
    4 of UNKNOWN~
    ~~start #2~~
    4 of UNKNOWN
    4 of UNKNOWN
    4 of UNKNOWN
    ~~end #2~~
    5 of UNKNOWN~
    ~~start #2~~
    5 of UNKNOWN
    5 of UNKNOWN
    5 of UNKNOWN
    5 of UNKNOWN
    ~~end #2~~
    6 of UNKNOWN~
    ~~start #2~~
    6 of UNKNOWN
    6 of UNKNOWN
    6 of UNKNOWN
    6 of UNKNOWN
    6 of UNKNOWN
    ~~end #2~~
    7 of UNKNOWN~
    ~~start #2~~
    7 of UNKNOWN
    7 of UNKNOWN
    7 of UNKNOWN
    7 of UNKNOWN
    7 of UNKNOWN
    7 of UNKNOWN
    ~~end #2~~
    8 of UNKNOWN~
    ~~start #2~~
    8 of UNKNOWN
    8 of UNKNOWN
    8 of UNKNOWN
    8 of UNKNOWN
    8 of UNKNOWN
    8 of UNKNOWN
    8 of UNKNOWN
    ~~end #2~~
    9 of UNKNOWN~
    ~~start #2~~
    9 of UNKNOWN
    9 of UNKNOWN
    9 of UNKNOWN
    9 of UNKNOWN
    9 of UNKNOWN
    9 of UNKNOWN
    9 of UNKNOWN
    9 of UNKNOWN
    ~~end #2~~
    10 of UNKNOWN~
    ~~start #2~~
    10 of UNKNOWN
    10 of UNKNOWN
    10 of UNKNOWN
    10 of UNKNOWN
    10 of UNKNOWN
    10 of UNKNOWN
    10 of UNKNOWN
    10 of UNKNOWN
    10 of UNKNOWN
    ~~end #2~~
    Jack of UNKNOWN~
    ~~start #2~~
    Jack of UNKNOWN
    Jack of UNKNOWN
    Jack of UNKNOWN
    Jack of UNKNOWN
    Jack of UNKNOWN
    Jack of UNKNOWN
    Jack of UNKNOWN
    Jack of UNKNOWN
    Jack of UNKNOWN
    Jack of UNKNOWN
    ~~end #2~~
    Queen of UNKNOWN~
    ~~start #2~~
    Queen of UNKNOWN
    Queen of UNKNOWN
    Queen of UNKNOWN
    Queen of UNKNOWN
    Queen of UNKNOWN
    Queen of UNKNOWN
    Queen of UNKNOWN
    Queen of UNKNOWN
    Queen of UNKNOWN
    Queen of UNKNOWN
    Queen of UNKNOWN
    ~~end #2~~
    King of UNKNOWN~
    ~~start #2~~
    King of UNKNOWN
    King of UNKNOWN
    King of UNKNOWN
    King of UNKNOWN
    King of UNKNOWN
    King of UNKNOWN
    King of UNKNOWN
    King of UNKNOWN
    King of UNKNOWN
    King of UNKNOWN
    King of UNKNOWN
    King of UNKNOWN
    ~~end #2~~
    Ace of UNKNOWN~
    ~~start #2~~
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    Ace of UNKNOWN
    ~~end #2~~
    ~end #1~
    size: 13
    It has to be rewriting the previous inputs, but I dont know why. I am probably missing something very obvious...

    --- Update ---

    Quote Originally Posted by copeg View Post
    Oh wow. That was silly. It might have had something to do with me having the wrong definition of static in my head. Haha. Thanks! That solved the problem, now that I actually read the definition of a static modifier! hahaha! I feel really stupid now... Thanks again!

    --- Update ---

    If anyone stumbles across this, and wants to know that final debugged code:
    Deck.java (snippet)
    public static ArrayList<Card> deck = new ArrayList();
     
    	public Deck(){
    		/*for(int suit = 1; suit < 5; suit++){
    			for(int id = 1; id < 15; id++){
    				//System.out.println( new Card(id, suit).toString() );
    				deckName.add( new Card(id, suit).toString() );
    				deck.add( new Card(id, suit) );
    			}
    		}*/
    		for(int suitNum = 1; suitNum < 5; suitNum++){
    			for(int id = 2; id < 15; id++){ // get int 1 through 13
    				deck.add( new Card( id, suitNum) ); //add new Card to deck
    			}
    		}
    		System.out.println( "size: " + deck.size() );
     
     
    	}

    Card.java
    import javax.swing.ImageIcon;
     
     
    public class Card {//extends JLabel
     
    	public int id;
    	public String suit, name, iconString;
    	private ImageIcon icon;
     
     
    	public Card(int id, int suitNum){
    		this.id = id;
     
    		suitType(suitNum);
    		idType(id);
     
     
    	}
     
    	private void suitType(int suitNum){
    		if(suitNum == 1){
    			suit = "Diamonds";
    		}else if(suitNum == 2){
    			suit = "Hearts";
    		}else if(suitNum == 3){
    			suit = "Spades";
    		}else if(suitNum == 4){
    			suit = "Clubs";
    		}else{
    			suit = "UNKNOWN";
    		}
    	}
    	private void idType(int id){
    		if(id == 11){
    			name = "Jack" + " of " + suit;
    		}else if(id == 12){
    			name = "Queen" + " of " + suit;
    		}else if(id == 13){
    			name = "King" + " of " + suit;
    		}else if(id == 14){
    			name = "Ace" + " of " + suit;
    		}else{
    			name = id + " of " + suit;
    		}	
    	}
     
    	@Override
    	public String toString(){
    		return name;
    	}
     
    	public void setID(int id){
    		this.id = id;
    		idType(id);
    	}
    	public void setSuit(int suitNum){
    		suitType(suitNum);
    	}
     
     
    	/** set icon images */
    	private void iconSet(int id, int suitNum){
    		/*for(int i = 2; i < 15; i ++){ // id
    			for(int j = 1; j < 5; j++){ // suit
    				if(id == i){
     
    				}
    			}
    		}*/
    		// set id + suit = icon1.png
    		iconString = suit + id + ".png"; // suitID = Diamonds2.png
    		icon = new ImageIcon(getClass().getResource("Resources/" + iconString));
    		//this.setText(icon);
    	}
     
    }

Similar Threads

  1. Working on a phdgets Project for Computer Science Need some Help.
    By Tallonejosh in forum What's Wrong With My Code?
    Replies: 21
    Last Post: April 25th, 2012, 08:39 AM
  2. Need help with Deck of Cards
    By yanksin1st in forum Java Theory & Questions
    Replies: 1
    Last Post: March 1st, 2012, 01:28 PM
  3. A deck of cards
    By glebovg in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 1st, 2012, 09:46 AM
  4. help on war card game project
    By sc0field1 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 3rd, 2011, 04:51 AM
  5. Adding cards back into deck
    By sp4ce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 15th, 2011, 01:21 AM