Created
May 14, 2013 03:24
-
-
Save k0emt/5573435 to your computer and use it in GitHub Desktop.
Example of working with enums in Java and in particular how to transform them into strings.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EnumStringExample { | |
public static enum DAYS_OF_THE_WEEK { | |
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY; | |
public String toString() { | |
String plainName = name().toString(); | |
String fancy = plainName.charAt(0) | |
+ plainName.substring(1).toLowerCase(); | |
return fancy; | |
} | |
} | |
public static void main(String[] args) { | |
System.out.println("implicit: " + DAYS_OF_THE_WEEK.MONDAY); | |
System.out.println("explicit: " + DAYS_OF_THE_WEEK.TUESDAY.toString()); | |
for (DAYS_OF_THE_WEEK day : DAYS_OF_THE_WEEK.values()) { | |
System.out.print(day); | |
switch (day) { | |
case MONDAY: | |
case TUESDAY: | |
case WEDNESDAY: | |
case THURSDAY: | |
case FRIDAY: | |
System.out.println(" weekday"); | |
break; | |
case SATURDAY: | |
case SUNDAY: | |
System.out.println(" weekend! :^D"); | |
break; | |
default: | |
System.out.println(" SAY WHAT!"); | |
break; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment