If you want convert enum string to enum in java you need use valueOf method like:
NameOfEnumType.valueOf(StringToConvert.toUpperCase());
- NameOfEnumType - An enum type (like class name )
- StringToConvert - string that will be converted to Enum
- toUpperCase() - is optional but by convention fields names are upper case,so this method will automatically convert to uppercase
Enum:
public enum Visa {
ALL,
WORK,
SLAVERY
STUDY,
NONE
}
so now, in place, where you need convert where you will use method like
String visatype = "terrorist";
try{
result = BorderAgency.isLegal();
}catch(IllegalArgumentException iae){
sendCitizenToJail(Visa.valueOf(visatype.toUpperCase()));
}
Source: http://download.oracle.com/javase/tutorial/java/javaOO/enum.html
No comments:
Post a Comment