Possible Duplicate:
Java - Convert String to enum
I have a method that uses an enum:
mymethod(AnotherClass.MyEnum.PassedEnum);
And I want to nest this within a class that receives a String which becomes MyEnum:
public static void method(String toPass){
mymethod(AnotherClass.toPass.PassedEnum);
}
The passed variable has to be a String but I need to convert it to a Enum to pass to AnotherClass?
TIA
You can create Enum from String by using Enum. valueOf() method. valueOf() is a static method that is added on every Enum class during compile-time and it's implicitly available to all Enum along with values(), name(), and cardinal() methods.
To convert string to enum use static method Enum. Parse. Parameters of this method are enum type, the string value and optionally indicator to ignore case.
The valueOf() method of the Enum class in java accepts a String value and returns an enum constant of the specified type.
The case-sensitive string representation of the enumeration name or underlying value to convert. When this method returns, contains an object of type TEnum whose value is represented by value if the parse operation succeeds.
Use AnotherClass.MyEnum.valueOf(toPass)
Do you mean like ...
MyEnum e = MyEnum.valueOf(text);
or
MyEnum e = Enum.valueOf(MyEnum.class, text);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With