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.


>> REGISTER NOW TO START POSTING


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
    		user_month=keyboard.nextLine();
     
    		//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
    Location
    Washington, DC
    Posts
    5,172
    My Mood
    Hungover
    Thanks
    141
    Thanked 600 Times in 515 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);
    //etc
     
    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
    Location
    Charleston SC
    Posts
    9
    My Mood
    Inspired
    Thanks
    0
    Thanked 0 Times in 0 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.