Results 1 to 4 of 4
Thread: Problem using enumerated types
- 10-10-2009, 02:20 AM #1
Member
- Join Date
- Sep 2009
- Posts
- 5
- Rep Power
- 0
Problem using enumerated types
Alright, here's my code:
And here's my output: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 + "."); } } }
Now my problem is I want my output to be of the format "The cards in my hand are:Java Code:The cards in my hand are: TEN of null. TEN of null. TEN of null. TEN of null. TEN of null.
RANK of SUIT," but why the heck is it giving me this output?Last edited by Stryker4526; 10-10-2009 at 02:27 AM.
- 10-10-2009, 02:38 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
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.
- 10-10-2009, 02:42 AM #3
Member
- Join Date
- Sep 2009
- Posts
- 5
- Rep Power
- 0
- 10-10-2009, 03:57 AM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
Similar Threads
-
generic types
By jon80 in forum New To JavaReplies: 6Last Post: 06-12-2009, 10:29 PM -
Collection Types
By DavidG24 in forum New To JavaReplies: 2Last Post: 04-18-2009, 05:03 PM -
WHy does it say imcompatable types?
By PeterFeng in forum New To JavaReplies: 3Last Post: 01-14-2009, 05:54 PM -
problem with scanner class:incompatible types
By fred in forum New To JavaReplies: 1Last Post: 07-20-2007, 07:02 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks