Example. A Month from User Input

[comp1110-2H]
void main(String args[]) { int month = Integer.parseInt(args[0]); Month m; if (month == 1) { m = Month.JANUARY; } else if (month == 2) { m = Month.FEBRUARY; } else if (month == 3) { m = Month.MARCH; } else if (month == 4) { m = Month.APRIL; } else if (month == 5) { m = Month.MAY; } else if (month == 6) { m = Month.JUNE; } else if (month == 7) { m = Month.JULY; } else if (month == 8) { m = Month.AUGUST; } else if (month == 9) { m = Month.SEPTEMBER; } else if (month == 10) { m = Month.OCTOBER; } else if (month == 11) { m = Month.NOVEMBER; } else { m = Month.DECEMBER; } IO.println(m.toString()); }

The final line in the above function also demonstrates that enum types have a built in .toString() method that converts to the string representation of the constant name.

bars magnifying-glass caret-down plus minus arrow-right xmark