Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By ExX

Thread: Need help with Enum

  1. #1
    ExX
    ExX is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Need help with Enum

    Hi I'm new around and I'm actually doing a java homework right now, it asks me to write an application that contains an enumeration that represents the days of week.
    My first objective in the program is to display a list of the days,
    Then I am suppose to prompt the user for a day,
    And finally display the hours of the chosen day.

    I'm not really sure what to do here since I am getting an error compiling this

    Java Code:
    import java.util.Scanner;
    public class DayOfWeek
    {
    	enum Days {MON, TUE, WED, THU, FRI, SAT, SUN}
    	
    	public static void main(String[] args)
    	{
    		String entry;
    		Scanner scan = new Scanner(System.in);
    		
    		System.out.println("Enter a day: ");
    		entry = scan.nextLine();		
    		switch(entry)
    		{
    			case MON:
    				System.out.println("Business hour from 9 to 9");
    				break;
    			case TUE:
    				System.out.println("Business hour from 9 to 9");
    				break;
    			case WED:
    				System.out.println("Business hour from 9 to 9");
    				break;
    			case THU:
    				System.out.println("Business hour from 9 to 9");
    				break;				
    			case FRI:
    				System.out.println("Business hour from 9 to 9");
    				break;
    			case SAT:
    				System.out.println("Business hour from 9 to 6");
    				break;
    			case SUN:
    				System.out.println("Business hour from 11 to 5");
                                    break;
                            default:
    	                        System.out.println("No such day exist!");
    		}
    		
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Need help with Enum

    I am getting an error compiling this
    And would you like to share the details of the error, or are we playing guessing games here?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Need help with Enum

    Nevertheless, a hint for you: what does Scanner#nextLine() return?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    ExX
    ExX is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Need help with Enum

    Oh sorry here is the error:
    Java Code:
    DayOfWeek.java:21: error: cannot find symbol
    			case MON:
    			     ^
      symbol:   variable MON
      location: class DayOfWeek
    DayOfWeek.java:24: error: cannot find symbol
    			case TUE:
    			     ^
      symbol:   variable TUE
      location: class DayOfWeek
    DayOfWeek.java:27: error: cannot find symbol
    			case WED:
    			     ^
      symbol:   variable WED
      location: class DayOfWeek
    DayOfWeek.java:30: error: cannot find symbol
    			case THU:
    			     ^
      symbol:   variable THU
      location: class DayOfWeek
    DayOfWeek.java:33: error: cannot find symbol
    			case FRI:
    			     ^
      symbol:   variable FRI
      location: class DayOfWeek
    DayOfWeek.java:36: error: cannot find symbol
    			case SAT:
    			     ^
      symbol:   variable SAT
      location: class DayOfWeek
    DayOfWeek.java:39: error: cannot find symbol
    			case SUN:
    			     ^
      symbol:   variable SUN
      location: class DayOfWeek
    7 errors
    
     ----jGRASP wedge2: exit code for process is 1.
     ----jGRASP: operation complete.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with Enum

    As Darryl states, what does Scanner#nextLine() return? You're trying to compare this with your Days enum, and that won't work. You can only compare apples to apples and oranges to oranges.

  6. #6
    ExX
    ExX is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Need help with Enum

    Oh okay my scanner was scanning for string, and i did it as:
    String entry;
    when I should have declared it as:
    Days entry;

    I got it to work now thanks for the hint :D
    Corrected code:
    Java Code:
    import java.util.Scanner;
    public class DayOfWeek
    {
    	enum Days {MON, TUE, WED, THU, FRI, SAT, SUN}
    	
    	public static void main(String[] args)
    	{
    		Days entry;
    		Scanner scan = new Scanner(System.in);
    		
    		System.out.println("Enter a day: ");
    		entry = Days.valueOf(scan.nextLine());		
    		switch(entry)
    		{
    			case MON:
    				System.out.println("Business hour from 9 to 9");
    				break;
    			case TUE:
    				System.out.println("Business hour from 9 to 9");
    				break;
    			case WED:
    				System.out.println("Business hour from 9 to 9");
    				break;
    			case THU:
    				System.out.println("Business hour from 9 to 9");
    				break;				
    			case FRI:
    				System.out.println("Business hour from 9 to 9");
    				break;
    			case SAT:
    				System.out.println("Business hour from 9 to 6");
    				break;
    			case SUN:
    				System.out.println("Business hour from 11 to 5");	
    		}
    		
    	}
    }
    Fubarable likes this.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Need help with Enum

    Glad you've got it working.

    You might consider showing the the options to the user before they enter it so they don't enter a wrong String.
    Even better is to catch errors before they mess up your program. Consider calling the Days.valueOf(...) in a try/catch block where you catch an IllegalArgumentException.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,244
    Rep Power
    19

    Default Re: Need help with Enum

    Java Code:
            switch(entry)
            {
                case MON:
                case TUE:
                case WED:
                case THU:
                case FRI:
                    System.out.println("Business hour from 9 to 9");
                    break;
                case SAT:
                    System.out.println("Business hour from 9 to 6");
                    break;
                case SUN:
                    System.out.println("Business hour from 11 to 5");   
            }
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    ExX
    ExX is offline Member
    Join Date
    Feb 2012
    Posts
    11
    Rep Power
    0

    Default Re: Need help with Enum

    Quote Originally Posted by Fubarable View Post
    Glad you've got it working.

    You might consider showing the the options to the user before they enter it so they don't enter a wrong String.
    Even better is to catch errors before they mess up your program. Consider calling the Days.valueOf(...) in a try/catch block where you catch an IllegalArgumentException.
    I have't learn that in class yet and i fixed it so my final version is like this:
    Java Code:
    import java.util.Scanner;
    public class DayOfWeek
    {
    	enum Days {MON, TUE, WED, THU, FRI, SAT, SUN}
    	
    	public static void main(String[] args)
    	{
    		Days day;
    		String[] days = new String[7];
    		days[0] = "MON";
    		days[1] = "TUE";
    		days[2] = "WED";
    		days[3] = "THU";
    		days[4] = "FRI";
    		days[5] = "SAT";
    		days[6] = "SUN";
    		Scanner scan = new Scanner(System.in);
    		
    		for(int i=0;i<days.length;i++)
    		{
    			System.out.println(days[i]);
    		}
    		System.out.println();
    		System.out.println("Enter a day: ");
    		day = Days.valueOf(scan.nextLine());		
    		switch(day)
    		{
      			case MON:
       	                case TUE:
      		        case WED:
     	                case THU:
        		        case FRI:
           		             System.out.println("Business hour from 9 to 9");
                                 break;
        		        case SAT:
            		     System.out.println("Business hour from 9 to 6");
            		     break;
        		        case SUN:
            		     System.out.println("Business hour from 11 to 5");   
    		}		
    	}
    }
    output came out like this:
    ----jGRASP exec: java DayOfWeek

    MON
    TUE
    WED
    THU
    FRI
    SAT
    SUN

    Enter a day:
    SAT
    Business hour from 9 to 6

    ----jGRASP: operation complete.

Similar Threads

  1. public static enum vs enum class
    By Dipke in forum New To Java
    Replies: 3
    Last Post: 08-30-2011, 10:45 AM
  2. Setting values from One Enum type to another enum type.
    By reach2sudhakar in forum New To Java
    Replies: 3
    Last Post: 09-23-2010, 06:02 PM
  3. enum
    By billq in forum New To Java
    Replies: 3
    Last Post: 01-03-2010, 08:38 PM
  4. Enum example
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-17-2008, 07:34 PM
  5. Enum?
    By vgbhagavan in forum Advanced Java
    Replies: 0
    Last Post: 06-14-2007, 02:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •