Results 1 to 17 of 17
Like Tree1Likes
  • 1 Post By aconti

Thread: java switch case

  1. #1
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default java switch case

    Hello,

    can I specify a range for each case example:

    case 1 - 3:{

    System.out.print("Very low mark"); }


    Thanks
    uthaya likes this.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    In some languages you can but in Java you cannot. What you do instead is omit the break statement.
    Java Code:
    switch number {
        case 1:
        case 2:
        case 3:
            // do stuff
            break;
        case 4:
        case 5:
            // etc
    }

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    No, Java (nor C++ nor C) alloq that notation; you have to list each possibility separate, e.g.

    Java Code:
    switch (x) {
       case 1:
       case 2:
       case 3: ...
    This is undoable for large ranges and you have to change your statement to a sequence of if-else-if-else ... statements.

    kind regards,

    Jos

    edit: Monday morning and too slow again ...
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    but I have all these different values:

    0 - 49.5
    50 - 59.5

    and so on...

    so I will use the if else statement

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Yep, if you have too many cases then an if statement is a better choice.

  6. #6
    aconti is offline Member
    Join Date
    May 2011
    Posts
    39
    Rep Power
    0

    Default

    thanks a lot very quick !

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by aconti View Post
    but I have all these different values:

    0 - 49.5
    50 - 59.5

    and so on...

    so I will use the if else statement
    Yep, that's the procedural approach; you could try a more object oriented approach: define a class (type) Range and store your Range objects in a Map<Range, Runnable>. If a Range is found you can run the associated Runnable. You could give it a try ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by aconti View Post
    Hello,

    can I specify a range for each case example:

    case 1 - 3:{

    System.out.print("Very low mark"); }


    Thanks
    Since the case only works with int variables use if statements, it will work on many types. Also if you have a MUCH larger range the case statements will be many.

    number is type double in the example below.

    Java Code:
    if( number >= 1.0 || number <= 40.0 )
        System.out.println( "Very low mark" );
    if( number >= 41.0 || number <= 70.0 )
        System.out.println( "Low mark" );
    if( number >= 71.0 || number <= 100.0 )
        System.out.println( "Better mark" );
    Last edited by stchman; 08-09-2011 at 12:29 AM.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Looks like I actually helped someone in this thread. Are you happy now?

  10. #10
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Yes, and you even managed to curb your urge to berate them as well. There might be hope yet.
    If you aren't programming in Java, well that's just too bad.
    I'd rather be using Ubuntu.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by stchman View Post
    Java Code:
    if( number >= 1.0 || number <= 40.0 )
        System.out.println( "Very low mark" );
    if( number >= 41.0 || number <= 70.0 )
        System.out.println( "Low mark" );
    if( number >= 71.0 || number <= 100.0 )
        System.out.println( "Better mark" );
    Please check your code before you post it. You want && instead of ||

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    I think you could probably do
    case > 0; case < 4:

    just a guess, but it might work

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Don't make guesses and provide incorrect advice. Try it yourself first before posting.

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by anthropamorphic View Post
    I think you could probably do
    case > 0; case < 4:

    just a guess, but it might work
    If you´re not sure whether or not your suggestion is correct, you should try it; your suggestion is not correct.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  15. #15
    anthropamorphic's Avatar
    anthropamorphic is offline Senior Member
    Join Date
    Jun 2011
    Posts
    135
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    If you´re not sure whether or not your suggestion is correct, you should try it; your suggestion is not correct.

    kind regards,

    Jos
    I Dont have the means to try it right now so my bad, but I don't know what I was think it would probably be something more like an and symbol instead of ;
    Case > 0 & case < 4:

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by anthropamorphic View Post
    I Dont have the means to try it right now so my bad, but I don't know what I was think it would probably be something more like an and symbol instead of ;
    Case > 0 & case < 4:
    Don't guess; Java (nor C, nor C++) work that way.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    WTF?

    Did you bother to read any of the replies before you posted?

    In Java YOU CANNNOT USE RANGES IN A SWITCH STATEMENT. So just stop making wild guesses when you don't have a clue what you are talking about.

Similar Threads

  1. Converting if to Switch Case.
    By king2be98 in forum New To Java
    Replies: 5
    Last Post: 02-20-2011, 03:46 PM
  2. Switch Case statement
    By seanfmglobal in forum New To Java
    Replies: 7
    Last Post: 02-15-2011, 01:18 PM
  3. if else changes to switch-case?
    By noobinoo in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 05:56 PM
  4. Replies: 2
    Last Post: 02-23-2009, 12:34 AM
  5. Switch Case and Key Events
    By AndrewM16921 in forum New To Java
    Replies: 4
    Last Post: 01-26-2009, 11:20 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
  •