Results 1 to 16 of 16
Like Tree2Likes
  • 2 Post By jlczuk

Thread: More with the Switch Statement

  1. #1
    Everyman's Avatar
    Everyman is offline Member
    Join Date
    Feb 2012
    Location
    Everywhere
    Posts
    26
    Rep Power
    0

    Default 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:
    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");
    }
    I thought I knew what I was doing (and that may not be correct), but this is what MyProgrammingLab thinks:

    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
    Can anyone help me with this problem?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default 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!

  3. #3
    Everyman's Avatar
    Everyman is offline Member
    Join Date
    Feb 2012
    Location
    Everywhere
    Posts
    26
    Rep Power
    0

    Default Re: More with the Switch Statement

    Quote Originally Posted by KevinWorkman View Post
    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)
    Thanks. I am re-writing the code now.

  4. #4
    Everyman's Avatar
    Everyman is offline Member
    Join Date
    Feb 2012
    Location
    Everywhere
    Posts
    26
    Rep Power
    0

    Default Re: More with the Switch Statement

    Quote Originally Posted by KevinWorkman View Post
    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)
    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:

    CTest.java:46: illegal start of expression
    What exactly is illegal? For future references...

  5. #5
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default 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!

  6. #6
    Beattie282 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default 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

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default 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!

  8. #8
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default 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.
    Java Code:
    switch (age)
    {
    case 0: 
    case 1:
      System.out.println("ineligible");
      break;
     ...
    </opinion>

  9. #9
    Beattie282 is offline Member
    Join Date
    Mar 2012
    Posts
    12
    Rep Power
    0

    Default Re: More with the Switch Statement

    Quote Originally Posted by KevinWorkman View Post
    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.
    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

  10. #10
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: More with the Switch Statement

    Quote Originally Posted by Beattie282 View Post
    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 :)
    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.

    Quote Originally Posted by Beattie282 View Post
    i got taught when ever you can take a choice of the user do it because their idots so i like to simplify things.
    "Never attribute to malice that which is adequately explained by stupidity." - Hanlon's razor - Wikipedia, the free encyclopedia
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: More with the Switch Statement

    Quote Originally Posted by Beattie282 View Post
    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.
    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.
    DarrylBurke and Everyman like this.

  12. #12
    Everyman's Avatar
    Everyman is offline Member
    Join Date
    Feb 2012
    Location
    Everywhere
    Posts
    26
    Rep Power
    0

    Default 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");
    }

  13. #13
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default 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?

  14. #14
    Everyman's Avatar
    Everyman is offline Member
    Join Date
    Feb 2012
    Location
    Everywhere
    Posts
    26
    Rep Power
    0

    Default Re: More with the Switch Statement

    Quote Originally Posted by jlczuk View Post
    Everyman,

    Do you think that you really need case 0 and 1, or are those perhaps already covered by another case?
    Actually, now that I think about it, its not absolutely necessary is it? Wouldn't that just be wasting space?

  15. #15
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default 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
    Java Code:
    case 2: case 3:
    to
    Java 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.

  16. #16
    Everyman's Avatar
    Everyman is offline Member
    Join Date
    Feb 2012
    Location
    Everywhere
    Posts
    26
    Rep Power
    0

    Default 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

  1. The Switch Statement
    By Everyman in forum New To Java
    Replies: 11
    Last Post: 04-20-2012, 03:29 PM
  2. Replies: 2
    Last Post: 03-26-2012, 04:27 PM
  3. switch statement
    By droidus in forum New To Java
    Replies: 2
    Last Post: 09-21-2011, 09:54 AM
  4. help with switch statement
    By java__beginner in forum New To Java
    Replies: 4
    Last Post: 03-19-2009, 02:22 PM
  5. Switch Statement Help
    By bluegreen7hi in forum New To Java
    Replies: 6
    Last Post: 02-06-2008, 05:16 AM

Posting Permissions

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