Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Enumerations help

  1. #1

    Default Enumerations help

    I have created an enumerated data type that represents months. In my program I have asked the user for the month. I hold the month entered by user into a string variable. From that string variable I'm trying to display the month using my enumerated data type. I know I can use if statements, but is there another simple way to do it?

    import java.util.Scanner;
    public class demo {
    	//Enum class called month that has constants in it representing months
    	enum month{january,february,march,april,may,june, july,august,september,
    		october,november, december};
    	public static void main(String[] args)
    		//Create a scanner object that reads from the user
    		Scanner keyboard=new Scanner(System.in);
    		//Ask user for month
    		System.out.print("Type Month: ");
    		//String variable to hold what month user types
    		String user_month;
    		//user types in their month
    		//how can i display the month on he console?
    		System.out.print(month.user_month);//error is on month.user_month
    											//user_month cannot be resolved

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Washington, DC
    My Mood
    Thanked 636 Times in 540 Posts

    Default Re: Enumerations help

    You could use a Map Strings to Months.

    Map<String, Month> monthMap = new HashMap<String, Month>();
    monthMap.put("January", Month.january);
    String userMonth = "January";
    Month enumMonth = monthMap.get(userMonth); //returns Month.january
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    May 2013
    Charleston SC
    Thanked 8 Times in 7 Posts

    Default Re: Enumerations help

    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.