Results 1 to 4 of 4
  1. #1
    Stryker4526 is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default Problem using enumerated types

    Alright, here's my code:
    Java Code:
    package edu.grcc.co117.game.cards;
    
    public class Card {
    	public static Rank rank;
    	public static Suit suit;
    
    	public Card() {
    		this(Rank.ACE, Suit.SPADES);
    	}
    
    	public Card(Rank rank, Suit suit) {
    		Card.rank = rank;
    		Card.suit = suit;
    	}
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		Card hand[] = { new Card(rank, suit), new Card(Rank.KING, suit),
    				new Card(Rank.QUEEN, suit), new Card(Rank.JACK, suit),
    				new Card(Rank.TEN, suit) };
    		System.out.println("The cards in my hand are: ");
    		for (int i = 0; i < hand.length; i++) {
    			System.out.println(hand[i].rank + " of " + hand[i].suit + ".");
    		}
    
    	}
    }
    And here's my output:
    Java Code:
    The cards in my hand are: 
    TEN of null.
    TEN of null.
    TEN of null.
    TEN of null.
    TEN of null.
    Now my problem is I want my output to be of the format "The cards in my hand are:
    RANK of SUIT," but why the heck is it giving me this output?
    Last edited by Stryker4526; 10-10-2009 at 03:27 AM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    You have made both rank and suit static: that is there is only one value of Rank and Suit that all cards will share.

    The last card that was added to the array ("new Card(Rank.TEN, suit)") sets this shared Rank value to TEN hence all of the cards report this as their rank when you print them. You don't set the static suit value anywhere, so it has the default value null.

    Change rank and suit so that they are no longer static. Then fix main() so that it does whatever it is supposed to.

  3. #3
    Stryker4526 is offline Member
    Join Date
    Sep 2009
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    You have made both rank and suit static: that is there is only one value of Rank and Suit that all cards will share.

    The last card that was added to the array ("new Card(Rank.TEN, suit)") sets this shared Rank value to TEN hence all of the cards report this as their rank when you print them. You don't set the static suit value anywhere, so it has the default value null.

    Change rank and suit so that they are no longer static. Then fix main() so that it does whatever it is supposed to.
    Ah... thanks, got it.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

Similar Threads

  1. generic types
    By jon80 in forum New To Java
    Replies: 6
    Last Post: 06-12-2009, 11:29 PM
  2. Collection Types
    By DavidG24 in forum New To Java
    Replies: 2
    Last Post: 04-18-2009, 06:03 PM
  3. WHy does it say imcompatable types?
    By PeterFeng in forum New To Java
    Replies: 3
    Last Post: 01-14-2009, 06:54 PM
  4. problem with scanner class:incompatible types
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 08:02 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •