Results 1 to 16 of 16
Thread: More with the Switch Statement
- 04-18-2012, 03:47 PM #1
More with the Switch Statement
Okay, last week I had problem similar to this with the switch statement:
"In the Happy Valley School System, children are classified by age as follows:
- less than 2, ineligible
- 2, toddler
- 3-5, early childhood
- 6-7, young reader
- 8-10, elementary
- 11 and 12, middle
- 13, impossible
- 14-16, high school
- 17-18, scholar
- greater than 18, ineligible
Given an int variable age , write a switch statement that prints out, on a line by itself, the appropriate label from the above list based on age."
My code:I thought I knew what I was doing (and that may not be correct), but this is what MyProgrammingLab thinks:Java Code:switch (age) { case x<2; System.out.println("ineligible"); break; case 3, 4, 5; System.out.println("early childhood"); break; case 6, 7; System.out.println("young reader"); break; case 8, 9, 10; System.out.println("elementary"); break; case 11, 12; System.out.println("middle"); break; case 13; System.out.println("impossible"); break; case 14, 15, 16; System.out.println("high school"); break; case 17, 18; System.out.println("scholar"); break; case 18<x; System.out.println("ineligible"); }
Can anyone help me with this problem?When we compiled your code with our code (to test for errors), the following compiler error messages were produced (red text, if present, can be clicked for additional information):
CTest.java:10: : expected
case x<2;
^
CTest.java:14: : expected
case 3, 4, 5;
^
CTest.java:18: : expected
case 6, 7;
^
CTest.java:22: : expected
case 8, 9, 10;
^
CTest.java:26: : expected
case 11, 12;
^
CTest.java:30: : expected
case 13;
^
CTest.java:34: : expected
case 14, 15, 16;
^
CTest.java:38: : expected
case 17, 18;
^
CTest.java:42: : expected
case 18<x;
^
9 errors
- 04-18-2012, 03:49 PM #2
Re: More with the Switch Statement
Much of that is not valid syntax for a switch statement. Recommended reading: The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-18-2012, 03:52 PM #3
Re: More with the Switch Statement
- 04-18-2012, 04:07 PM #4
Re: More with the Switch Statement
This is what I got:
Java Code:switch (age) { case 0: case 1: System.out.println("ineligible"); break; case 2: System.out.println("toddler"); break; case 3: case 4: case 5: System.out.println("early childhood"); break; case 6: case 7: System.out.println("young reader"); break; case 8: case 9: case 10: System.out.println("elementary"); break; case 11: case 12: System.out.println("middle"); break; case 13: System.out.println("impossible"); break; case 14: case 15: case 16: System.out.println("high school"); break; case 17: case 18: System.out.println("scholar"); break; case >18: System.out.println("ineligible"); }
But when I submit this to MyProgrammingLab, I get this:
What exactly is illegal? For future references...CTest.java:46: illegal start of expression
- 04-18-2012, 04:13 PM #5
Re: More with the Switch Statement
You're still using invalid syntax. You shouldn't use inequalities (<, >, ==, etc) in a switch statement.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-18-2012, 04:28 PM #6
Member
- Join Date
- Mar 2012
- Posts
- 12
- Rep Power
- 0
Re: More with the Switch Statement
// I removed my answer because it did no answer the question at hand, sorry
Last edited by Beattie282; 04-18-2012 at 05:34 PM. Reason: I removed my answer because it did no answer the question at hand, sorry
- 04-18-2012, 04:37 PM #7
Re: More with the Switch Statement
I don't think the above advice takes the point of the assignment into consideration. You are supposed to switch on an age, not a set of cases given some range of ages as predefined cases. I appreciate that you're trying to help, but you might be more mindful of what the question is actually asking before you post a full solution like that.
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-18-2012, 04:37 PM #8
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: More with the Switch Statement
To summarize, you needed to use ':' instead of ';' after each case statement and as pointed out, you cannot use any type of operation as part of the case statement.
Read the tutorial that KevinWorkman linked you to very carefully. The first paragraph tells you exactly what types you can use for your case statements.
<opinion>
I think switch statements are more readable and serviceable if you use this stylistic form versus having each of them side-by-side.
</opinion>Java Code:switch (age) { case 0: case 1: System.out.println("ineligible"); break; ...
- 04-18-2012, 04:46 PM #9
Member
- Join Date
- Mar 2012
- Posts
- 12
- Rep Power
- 0
Re: More with the Switch Statement
well if the same answer is given for a range of ages whats the point of having the other assignments? am assuming this is what you mean?
dont get me wrong i know what you mean say if the user puts in 5 it wont know what to do but i said if you are using a drop down menu :)
i got taught when ever you can take a choice of the user do it because their idots so i like to simplify things.Last edited by Beattie282; 04-18-2012 at 04:49 PM. Reason: typo
- 04-18-2012, 04:54 PM #10
Re: More with the Switch Statement
Right, that makes sense for a "real" program, but this is an assignment designed to explore switch statements. I believe the OP would get points taken off for doing it your way, as it goes around the original statement of the program.
"Never attribute to malice that which is adequately explained by stupidity." - Hanlon's razor - Wikipedia, the free encyclopediaHow to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 04-18-2012, 04:56 PM #11
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: More with the Switch Statement
Beattie,
Taking the original poster at their word, the assignment was very explicit. "Given an int variable age , write a switch statement that prints out, on a line by itself, the appropriate label from the above list based on age."
The OP's approach, with a few minor problems is concise and clear. By encoding age ranges beforehand, you've taken a simple requirement and made it more complex and thus more prone to error. Like using '<' instead of '>'. To handle the last case of "greater than 18", that would be satisfied quite simply as the default clause, assuming the teacher clarified that checking for inputs less than or equal to zero was not necessary.
Oh, and overloading the age variable like you did....very very very bad idea.
- 04-19-2012, 03:04 PM #12
Re: More with the Switch Statement
I got it! I got it!
Java Code:switch (age) { case 0: case 1: System.out.println("ineligible"); break; case 2: System.out.println("toddler"); break; case 3: case 4: case 5: System.out.println("early childhood"); break; case 6: case 7: System.out.println("young reader"); break; case 8: case 9: case 10: System.out.println("elementary"); break; case 11: case 12: System.out.println("middle"); break; case 13: System.out.println("impossible"); break; case 14: case 15: case 16: System.out.println("high school"); break; case 17: case 18: System.out.println("scholar"); break; default: //This was the main error. System.out.println("ineligible"); }
- 04-19-2012, 03:10 PM #13
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: More with the Switch Statement
Everyman,
Do you think that you really need case 0 and 1, or are those perhaps already covered by another case?
- 04-19-2012, 03:15 PM #14
Re: More with the Switch Statement
- 04-19-2012, 03:22 PM #15
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: More with the Switch Statement
It's not incorrect, just unnecessary. Your cases should only differentiate what they really need to and let the default take care of everything else.
It would be more readable if you changed from
toJava Code:case 2: case 3:
As I mentioned in a previous post, that is just a stylistic recommendation that makes your code easier to read. The goal is not necessarily to have the most compact code in terms of the number of lines.Java Code:case 2: case 3:
- 04-19-2012, 03:24 PM #16
Re: More with the Switch Statement
Thanks, man. My teacher is kinda hard on that stuff. And thanks to all that have assisted me thus far.
(I shall not forget you...)
Similar Threads
-
The Switch Statement
By Everyman in forum New To JavaReplies: 11Last Post: 04-20-2012, 03:29 PM -
the switch statement and unreachable statement error
By name in forum New To JavaReplies: 2Last Post: 03-26-2012, 04:27 PM -
switch statement
By droidus in forum New To JavaReplies: 2Last Post: 09-21-2011, 09:54 AM -
help with switch statement
By java__beginner in forum New To JavaReplies: 4Last Post: 03-19-2009, 02:22 PM -
Switch Statement Help
By bluegreen7hi in forum New To JavaReplies: 6Last Post: 02-06-2008, 05:16 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks