public class Demo {
public enum Month {
JANUARY(1), FEBRUARY(2), MARCH(3), APRIL(4), MAY(5), JUNE(6), JULY(7), AUGUST(8), SEPTEMBER(9), OCTOBER(10), NOVEMBER(11), DECEMBER(12);
int numMonth;
private Month(int numMonth) {
this.numMonth = numMonth;
}
};
public static void main(String[] args) {
//Enum value...
Month month;
//Read in Scanner
Scanner keyboard = new Scanner(System.in);
System.out.println("What Month?");
//Keyboard Entry
String userMonth;
userMonth = keyboard.nextLine();
//Displays what user types
System.out.println(String.format("User entered: %s", userMonth));
try { //use a try catch because Month.valueOf() throws IllegalArgumentException if it can't find the string.
month = Month.valueOf(userMonth.toUpperCase().trim());
System.out.println(String.format("Month: %s; Numeric Value of %s: %d", month.toString(), month.toString(), month.numMonth));
} catch (IllegalArgumentException ex) {
System.out.println("The user input wasn't a month");
}
}
}
I commented the code. This should help you with whatever you are doing.