This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes compare using enum and int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "compare using enum and int" Watch "compare using enum and int" New topic
Author

compare using enum and int

Clapa Danut
Greenhorn

Joined: May 05, 2011
Posts: 8
Hy guys,
i have a question:
lets suppose i have a enum

now i have a int variable databaseCodeCar that can contains values between 1 and 3 acordingly with the enum class above;
i want to do something like

how i can do that?
i can do CarTypes(databaseCodeCar)(or any method to select a particular constant from that class everything else but naming it dirctly?) to select the cosntant from that enum class?

[Edit - added code tags - see UseCodeTags for details]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6



However, what I'd do is add a static method to CarTypes to get a CarTypes instance from an int, and then I'd switch on that CarTypes object.

I'd also prefer the name CarType (singluar) over CarTypes (plural).
Clapa Danut
Greenhorn

Joined: May 05, 2011
Posts: 8
Jeff Verdegan wrote:


figured out finally in the searching process
never used enum and didnt expected to be so easy

dingggggg ty for help
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're welcome.

And welcome to the Ranch!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Clapa Danut wrote: . . .
. . .
Do you see how I have enhanced your enum and you can get rid of those dreadful if-elses.
You can get a number called ordinal from an enum member, and you can also get an array of those members. So you can use the ordinal to search the array. It would appear that the ordinal is equal to codeCar - 1.You can also get an enum member from its name with the valueOf() method. For more details look in the Enum class, call the values() method of your enum, and look in the Java Tutorials.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Campbell Ritchie wrote:Do you see how I have enhanced your enum and you can get rid of those dreadful if-elses.

Nice enhancement. Its reusable.


Mohamed Sanaulla | My Blog
Clapa Danut
Greenhorn

Joined: May 05, 2011
Posts: 8
Campbell Ritchie wrote:
Clapa Danut wrote: . . .
. . .
Do you see how I have enhanced your enum and you can get rid of those dreadful if-elses.
You can get a number called ordinal from an enum member, and you can also get an array of those members. So you can use the ordinal to search the array. It would appear that the ordinal is equal to codeCar - 1.You can also get an enum member from its name with the valueOf() method. For more details look in the Enum class, call the values() method of your enum, and look in the Java Tutorials.


Well beleve it or not that i tryed in the first time but i didnt figured out how to do this.
i have a variable int external lets suppose int external = 2;
basically what i want in this case is:
to indicate to CarTypes that i want the message from HONDA because my external int is 2.
something like CarType.getMessage(external == carCode 2).
to avoide if (external == CarCode.HONDA) printn ("BatMan");
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
Mohamed Sanaulla wrote: . . . Nice enhancement. Its reusable.
Thank you. It’s just object-oriented thinking. You can change the message with a Map
Clapa Danut
Greenhorn

Joined: May 05, 2011
Posts: 8
Campbell Ritchie wrote:
Mohamed Sanaulla wrote: . . . Nice enhancement. Its reusable.
Thank you. It’s just object-oriented thinking. You can change the message with a Map


So it isnt a straight solution as i thought. Thought at that but i beleved is wrong from the point of architectural view.

Thank you very much for help. You really solved me a problem
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Campbell Ritchie wrote:
Mohamed Sanaulla wrote: . . . Nice enhancement. Its reusable.
Thank you. It’s just object-oriented thinking.

Can't figure out if these statements are sincere or not. Like any class, the attributes of an enum should be things that truly belong to it. Strings that are relevant to a CarType are legitimate attributes, but not, IMO, Strings that represent arbitrary reactions to encountering one. Those properly belong outside of the enum definition.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

Dennis Deems wrote:
Campbell Ritchie wrote:
Mohamed Sanaulla wrote: . . . Nice enhancement. Its reusable.
Thank you. It’s just object-oriented thinking.

Can't figure out if these statements are sincere or not. Like any class, the attributes of an enum should be things that truly belong to it. Strings that are relevant to a CarType are legitimate attributes, but not, IMO, Strings that represent arbitrary reactions to encountering one. Those properly belong outside of the enum definition.

Agree for what you have said. If they are sincere and related to the CarType then they truly belong to the CarType attribute.
And in case they dont belong to the CarType, the other approach of using a Map can be applied.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38519
    
  23
I did mean it sincerely. And I showed examples of how those Strings can be inside the enum, ie belong to each CarType object, or outside the enum, ie simply associated with a CarType, which might be implemented with a Map.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: compare using enum and int