Results 1 to 9 of 9
Thread: Need help with Enum
- 02-04-2012, 07:11 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
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!"); } } }
- 02-04-2012, 07:14 PM #2
Re: Need help with Enum
And would you like to share the details of the error, or are we playing guessing games here?I am getting an error compiling this
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-04-2012, 07:15 PM #3
Re: Need help with Enum
Nevertheless, a hint for you: what does Scanner#nextLine() return?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 02-04-2012, 07:16 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
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.
-
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.
- 02-04-2012, 07:25 PM #6
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
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"); } } }
-
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.
- 02-04-2012, 07:30 PM #8
Re: Need help with Enum
dbJava 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"); }Why do they call it rush hour when nothing moves? - Robin Williams
- 02-04-2012, 07:56 PM #9
Member
- Join Date
- Feb 2012
- Posts
- 11
- Rep Power
- 0
Re: Need help with Enum
I have't learn that in class yet and i fixed it so my final version is like this:
output came out 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"); } } }
----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
-
public static enum vs enum class
By Dipke in forum New To JavaReplies: 3Last Post: 08-30-2011, 10:45 AM -
Setting values from One Enum type to another enum type.
By reach2sudhakar in forum New To JavaReplies: 3Last Post: 09-23-2010, 06:02 PM -
enum
By billq in forum New To JavaReplies: 3Last Post: 01-03-2010, 08:38 PM -
Enum example
By Java Tip in forum java.langReplies: 0Last Post: 04-17-2008, 07:34 PM -
Enum?
By vgbhagavan in forum Advanced JavaReplies: 0Last Post: 06-14-2007, 02:02 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks