• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

how to convert a string to a enum ?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
reply
    Bookmark Topic Watch Topic
  • New Topic