Results 1 to 14 of 14
  1. #1
    nomnum is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default SOLVED: Why can't Switch Case handle ranges of numbers?

    So I pretty much want to make it so that each switch case can cover multiple numbers so when it comes to something like age i don't have to make 100 switch cases and things would get repetitive. I.E. My age 37 response is the same as 38 and so on.
    So I made this just as a practice program and I am curious is there any Solution to making ranges of data with switch cases thats not mind blowing difficult?

    Why can't Switch Case handle ranges of numbers?-25g5c3l.jpg
    Last edited by nomnum; 08-15-2013 at 08:51 PM. Reason: Solved

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: Why can't Switch Case handle ranges of numbers?

    Quote Originally Posted by nomnum View Post
    So I pretty much want to make it so that each switch case can cover multiple numbers
    Better find a language that supports that, then. Java doesn't.

    You can use multiple if-else statements though.

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

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why can't Switch Case handle ranges of numbers?

    In Japanese there's a single word for 'the fear that your penis shrinks'; in Dutch no such word exists (for obvious reasons); similar with Java: the language doesn't support such construct but it offers other language constructs for alternatives ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    nomnum is offline Member
    Join Date
    Aug 2013
    Posts
    2
    Rep Power
    0

    Default Re: Why can't Switch Case handle ranges of numbers?

    Quote Originally Posted by JosAH View Post
    In Japanese there's a single word for 'the fear that your penis shrinks'; in Dutch no such word exists (for obvious reasons); similar with Java: the language doesn't support such construct but it offers other language constructs for alternatives ...

    kind regards,

    Jos
    lol, had to read it twice. Thanks for the reply :) Just our of curiosity, whats a common language that would support this.

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,057
    Rep Power
    6

    Default Re: Why can't Switch Case handle ranges of numbers?

    Quote Originally Posted by JosAH View Post
    in Dutch no such word exists (for obvious reasons);
    Yeah it can't shrink any further anyway.

    -- snip --
    Last edited by gimbal2; 08-16-2013 at 09:48 AM. Reason: needed more coffee to have a functioning brain
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: Why can't Switch Case handle ranges of numbers?

    Quote Originally Posted by nomnum View Post
    whats a common language that would support this.
    Any language that allows a boolean expression in case statements. Vax DataTrieve and xBase come to mind, but they're not exactly common.

    Would you really change to a different language just to get boolean condition support in case statements?

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

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why can't Switch Case handle ranges of numbers?

    Quote Originally Posted by gimbal2 View Post
    Yeah it can't shrink any further anyway.

    -- snip --
    You ate too many sushis and sashimis ...

    kind regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,057
    Rep Power
    6

    Default Re: Why can't Switch Case handle ranges of numbers?

    Quote Originally Posted by JosAH View Post
    You ate too many sushis and sashimis ...

    kind regards,

    Jos ;-)
    Barf!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    TechKid's Avatar
    TechKid is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Why can't Switch Case handle ranges of numbers?

    Im sorry to tell you the honest truth but the only way to get mulltiple reponses throught java is to make a generic method like this for example ill write for age stored in data value T

    make sure the method is out of main
    [CODE]
    public class Data {
    public static void main(Sting[] resp) {
    String[] {"answer", "resp", "r", "r2","a2"};
    Character[] {"firsta", "sa", "ta", "fa", "a5"};
    Integer[] {"0", "1", "-1"};
    }
    public static <T entends Comparable<T>> T(T[] r) {
    for ("%s ", r > 0) {
    System.out.println("user entered a key read by generic T");

    }
    return for (Integer[] 0 <= Integer[] -1) {
    System.out.printf(0, "%s \nTry entering something, Reload");
    System.out.println();
    System.exit(0);
    }
    }
    [ENDOFCODE]

    u get so if want a tutorial on it type on youtube java intermidate programming 18,17,19

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,057
    Rep Power
    6

    Default Re: Why can't Switch Case handle ranges of numbers?

    You should NEVER be sorry to tell the honest truth.

    So I have to tell you the honest truth: your reply and the code make no sense at all.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,574
    Blog Entries
    7
    Rep Power
    21

    Default Re: Why can't Switch Case handle ranges of numbers?

    Quote Originally Posted by gimbal2 View Post
    your reply and the code make no sense at all.
    i don't even know what the intention of that 'code' (mind the quotes) is supposed to be; it doesn't look like Java 8 closures; it doesn't look like one of those functional languages implemented on top of the jvm; it looks like gibberish to me ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    TechKid's Avatar
    TechKid is offline Member
    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Default Re: Why can't Switch Case handle ranges of numbers?

    Sorry next time i just wont post and say GOTO YOUBETUBE an type in thenewboston java for beginners or java intermidate playlist. Could you understand that code their?

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,305
    Rep Power
    20

    Default Re: Why can't Switch Case handle ranges of numbers?

    This is a Java forum, and that's not Java code. And even if it could be translated to Java, which I doubt, it would have nothing to do with the question asked here.

    Next time you have the urge to post irrelevant code at least find a forum for whatever programming language to code is in.

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

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,057
    Rep Power
    6

    Default Re: Why can't Switch Case handle ranges of numbers?

    Quote Originally Posted by TechKid View Post
    Sorry next time i just wont post and say GOTO YOUBETUBE an type in thenewboston java for beginners or java intermidate playlist. Could you understand that code their?
    I'm sure those videos helped you a lot when you watched them for the first time yesterday.
    Last edited by gimbal2; 08-16-2013 at 08:38 PM. Reason: its hard to read videos
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. [SOLVED] switch case with '<='
    By Adomini in forum New To Java
    Replies: 9
    Last Post: 02-17-2013, 04:01 PM
  2. Converting if to Switch Case.
    By king2be98 in forum New To Java
    Replies: 5
    Last Post: 02-20-2011, 03:46 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. Printing ranges of numbers using if/else statements
    By russbuss106 in forum New To Java
    Replies: 8
    Last Post: 03-05-2009, 05:03 AM
  5. Switch Case and Key Events
    By AndrewM16921 in forum New To Java
    Replies: 4
    Last Post: 01-26-2009, 11:20 PM

Tags for this Thread

Posting Permissions

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