wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Compare an Object to an Enum list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compare an Object to an Enum list" Watch "Compare an Object to an Enum list" New topic
Author

Compare an Object to an Enum list

Declan Dee
Greenhorn

Joined: May 04, 2010
Posts: 20
Greetings All,

My name is Declan and I am new to both Java programming, programming in general and of course this site.

I hope to get to know you all in the coming years (!)

My first question is this. I have the following two Enums:

public enum CardSuit { SPADES, CLUBS, DIAMONDS, HEARTS } and

public enum CardRank { DEUCE, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE }

I create an object (card1) with the following constructor:

public card (String suit, String rank) {
this.name = name;
this.rank = rank;
}

My question is, how do I compare the card1 object to these Enums?

For example if my object is Card1 (Spades, Six) and I create a second object (Hearts, Two), I want to have it that because SPADES is first (higher) in the Enum list that object1 (Card1) is automatically higher that object2 (Card2).

I have to write a program that compares the values of cards.

I have been working with CompareTo() with little success

Any help is greatly appreciated

Thanks in advance

Declan


rogel garcia
Ranch Hand

Joined: Dec 19, 2003
Posts: 41
You could do Something like that


But I think it is better to change the attributes of the class card to match the enums, ex:


SCJP 1.4
rogel garcia
Ranch Hand

Joined: Dec 19, 2003
Posts: 41
Using the Comparable interface...

Declan Dee
Greenhorn

Joined: May 04, 2010
Posts: 20
Hi Rogel.

Thank You

This should keep me busy for a while

Declan D
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compare an Object to an Enum list
 
Similar Threads
Logging problem and grammatical problems
Need Help in java
looping through an enum?
Trying to learn enumerated types of class
cannot read a Collection of a custom class as a parameter