This is not possible in Java as it is. But you can do your own implementation as work, although it will give more code. You can change your enum as follows:
public enum Model { IC19_01("IC19-01"), IC19_02("IC19-02") private final String name; private Model(String name){ this.name = name; } public String getName(){ return name; } public static Model getByName(String aName){ for(Model current: Model.values()){ if(current.getName().equalsIgnoreCase(aName.trim())){ return current; } } return null; } }
Then you can call Model.getByName(st) instead of Model.valueOf . Alternatively, in Java 7 you can switch the actual String .
source share