Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

The Best Way to Assign an Enum to the Corresponds value of a String?

807580Feb 4 2010 — edited Feb 4 2010
I am reading from and xml file configuration file. The data being returned is stored as a String and corresponds to a Enum value. My question is - What is the best way to assign the enum the value that corresponds to value of the string?
public enum Names { BOB, FRED, JIM, SAM }

String name = "FRED";   // This is the data retuned from the xml file.
Names n =              // I want to assign n the value of Names.FRED
I know I could do an "if" statement. However, this is a contrived example with only a few enum values. In the real world, this enum list could be quite large.
I have looked into reflection, but I don't know whether there is an easier way to achieve my objective?

Thank you for your time,
Harold Clements
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 4 2010
Added on Feb 4 2010
9 comments
299 views