• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to convert a string to a enum ?  RSS feed

 
Shazia Bashir
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

i have a question. Im working on a program, that reads files. In one of these files i have a car-code. Am wondering if i could use a enum type to set the car type. Example:



If i run this code i get exception when i run valueof... :


What is the best way to set the carcodes i read from to an to an enum or constant/object ?

Hope you understand my question

Thanks.

[Winston] Edited to split up long comment line and exception line.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't use the built-in valueOf, because that's looking for the identifier. But there's nothing stopping you adding your own equivalent. Something like this:

- Add a static Map to the class that will map from the code to the enum values.
- Add a static initialiser block to populate this Map
- Add a public static method that will convert from the code to the enum value
 
Shazia Bashir
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the great answer. Im new to programming, could you please write some code in how i can do it ? thanks.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shazia Bashir wrote:thanks for the great answer. Im new to programming, could you please write some code in how i can do it ? thanks.

That's not usually how we do things. Try out something yourself, and come back if you have any problems.

ALSO: While Matthew's suggestion is definitely the most generic, you could just do a brute-force search of Car.values() if you don't have very many; and the code to do that will be a lot simpler.

Winston
 
Shazia Bashir
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What do you mean by brute-force search ? make an for-loop and search through every time ?

is this the proper way to implement it ?

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shazia Bashir wrote:What do you mean by brute-force search ? make an for-loop and search through every time ?

Yes. Car.values() returns an array of all the Cars you've defined, and a search of a dozen array elements or so may actually be quicker than a HashMap retrieval. If you get to 50 you might want to rethink.

Mind you, you seem to be 80% of the way to implementing Mathhew's suggestion...

Winston
 
Shazia Bashir
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thanks Winston, is this the right way to implement the brute-force search ?

 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, this is it. I'd only replace your line 17
with - this way you don't need to touch it if you add or modify the constants in your enum.

Edit: actually, wrong! You're comparing strings with the == operator (line 22). You need to use the equals() method.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using the brute force approach, I wouldn't bother with the static variable. I'd just use:


(Edit: though the way you're doing it may actually be more efficient, though I'd argue less clear)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!