Stewart Clay Stewart Clay - 1 year ago 79
Java Question

Is this way of searching an enum acceptable

i am busy with enum's and was looking at a way to search an enum to see if a string a user enters is there, so i created this.


try {
String A = "DECc";
} catch(Exception e) {
System.out.println("Could not find an enum with the String you entered");

Answer Source

yes this is good.. the other option is "to string" all the enum elements and compare the string to them one by one:

for (Month m : Month.values()) {
    if ( {

but your way is really better...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download