+ Post New Thread
Results 1 to 8 of 8
Coding Thread, There has to be an easier way... in Coding and Web Development; So, I'm working through some Java stuff... CPD an' all that. And I'm up to a part about switch statements. ...
  1. #1

    X-13's Avatar
    Join Date
    Jan 2011
    Location
    /dev/null
    Posts
    9,095
    Thank Post
    592
    Thanked 1,953 Times in 1,351 Posts
    Blog Entries
    19
    Rep Power
    814

    There has to be an easier way...

    So, I'm working through some Java stuff... CPD an' all that.

    And I'm up to a part about switch statements. Seems easy enough to understand.

    I've got an exercise I'm trying to work on:

    Exercise
    Write a programme that accepts user input from the console. The programme should take a number and then test for the following age ranges: 0 to 10, 11 to 20, 21 to 30, 30 and over. Display a message in the Output window in the following format:
    user_age + " is between 21 and 30"
    So if the user enters 27 as the age, the Output window should be this:

    If the user is 30 or over, you can just display the following message:
    "Your are 30 or over"

    Help for this exercise
    To get string values from the user, you did this:
    String age = user_input.next( );
    But the next( ) method is used for strings. The age you are getting from the user has to be an integer, so you can't use next( ). There is, however, a similar method you can use: nextInt( ).
    The thing is... I ended up doing it with a lot of "else if" commands, but can't figure out how to do it WITH switch that doesn't make it a million times longer. Does anyone have any ideas?


    Also; I keep getting an error "strings in switch are not supported in -source 1.6 (use -source 7 or higher to enable strings in switch)" I'm using JDK 8... which comes up as 1.8, and is THE ONLY one I have installed.

    How do I get netbeans to recognise it?

  2. #2

    TechMonkey's Avatar
    Join Date
    Dec 2005
    Location
    South East
    Posts
    3,287
    Thank Post
    225
    Thanked 405 Times in 302 Posts
    Rep Power
    162
    My understanding is if you are using integers switch isn't a great choice as you can't evaluate directly. You would have evaluate first give an id number or string tag and then run that through the switch.

    For example (in pseudo code)
    if age <10 then ageRange = "under10"

    case under10:
    System.out.println("You are under 10");
    break;
    default:
    System.out.println("I have no clue how old you are");
    break;

    Otherwise you'd have to mention every case:

    case 0: case 1: case 2: case 3: etc etc etc

    Your final problem, I have no clue sorry.

  3. #3

    Join Date
    Jan 2013
    Posts
    98
    Thank Post
    23
    Thanked 11 Times in 10 Posts
    Rep Power
    5
    You can type convert in java by using the type libraries for ints the type library is Integer so you can use Integer.parseInt(<string value>) which will try and convert the string into integer value. You may have to surround this all in try catch as the string might not contain a value and what would happen then?

    In terms of switch statements, i've done java a while back pre 1.6 and switch only supported ints (and chars/short ints but we wont go into that), The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    It looks like they have changed switch in java 7/8 to support strings but to stop it from breaking older code they require a parameter to be passed to enable this. Looks like option is here:

    https://netbeans.org/kb/docs/java/javase-jdk7.html

    Hope this helps, keep coding

  4. #4

    X-13's Avatar
    Join Date
    Jan 2011
    Location
    /dev/null
    Posts
    9,095
    Thank Post
    592
    Thanked 1,953 Times in 1,351 Posts
    Blog Entries
    19
    Rep Power
    814
    Quote Originally Posted by TechMonkey View Post
    My understanding is if you are using integers switch isn't a great choice as you can't evaluate directly. You would have evaluate first give an id number or string tag and then run that through the switch.

    For example (in pseudo code)
    if age <10 then ageRange = "under10"

    case under10:
    System.out.println("You are under 10");
    break;
    default:
    System.out.println("I have no clue how old you are");
    break;

    Otherwise you'd have to mention every case:

    case 0: case 1: case 2: case 3: etc etc etc

    Your final problem, I have no clue sorry.
    That's pretty much how I did it.

    String check
    If _input <=10 {
    check = "a"}
    else if... blah

    Switch check
    case "a"
    Blah blah and so forth.

  5. #5

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,452
    Thank Post
    1,439
    Thanked 1,170 Times in 798 Posts
    Rep Power
    707
    Is there some reason you want to use switch statements? They only work with constants so are pretty awful for conditional numerical values. You can drop multiple cases in to a single statement by just not 'breaking' out of them, e.g.

    Code:
    switch(value){
        case 1: case 2: case 3:
        case 4: case 5: case 6:
        case 7: case 8: case 9:
            system.println("The value is less than 10");
            break;
    }
    ...but that's a horrible way to code.

    Stick to your if-else statements I'd say. Switch statements are only really suited to constants.

  6. #6

    X-13's Avatar
    Join Date
    Jan 2011
    Location
    /dev/null
    Posts
    9,095
    Thank Post
    592
    Thanked 1,953 Times in 1,351 Posts
    Blog Entries
    19
    Rep Power
    814
    Quote Originally Posted by LosOjos View Post
    Is there some reason you want to use switch statements? They only work with constants so are pretty awful for conditional numerical values. You can drop multiple cases in to a single statement by just not 'breaking' out of them, e.g.

    Code:
    switch(value){
        case 1: case 2: case 3:
        case 4: case 5: case 6:
        case 7: case 8: case 9:
            system.println("The value is less than 10");
            break;
    }
    ...but that's a horrible way to code.

    Stick to your if-else statements I'd say. Switch statements are only really suited to constants.
    That's what I was thinking... the exercise was after the switch part, so I was thinking it'd be relevant.

    Seems I've just found yet another place with no concept of when to put challenges...

  7. #7

    tmcd35's Avatar
    Join Date
    Jul 2005
    Location
    Norfolk
    Posts
    5,665
    Thank Post
    850
    Thanked 893 Times in 738 Posts
    Blog Entries
    9
    Rep Power
    328
    Long winded, but I think this is the answer the problem is looking for...

    Code:
    Int age = user_input.nextInt();
    
    Switch age {
        case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: System.out.println("You are under 10"); break;
        case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: System.out.println("You are under 20"); break;
        case 20: case 21: case 22: case 23: case 24: case 25: case 26: case 27: case 28: case 29: System.out.println("You are under 30"); break;
        default: System.out.println("You are over 30"); break;
    }
    Not very elegant. Can do the same job with a couple of IF's.

    edit: @LosOjos beat me to it, lol
    Last edited by tmcd35; 31st March 2014 at 03:37 PM.

  8. #8

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,696
    Thank Post
    335
    Thanked 515 Times in 483 Posts
    Rep Power
    179
    Only my personal thoughts, but any reason you don't do it like this?

    (Note not actual valid java)

    Code:
    switch(roundup(value/10)) {
    case 1: system.println("The value is 0-10");
    case 2: system.println("The value is 10-20");
    case 3: system.println("The value is 20-30");
    case else: system.println("The value is 30+");
    
    }
    -Edit no reason you can't put a statement in to shorten the possible case values Divide age by 10 rounded, and you only have a few options left.

    Steve
    Last edited by Steve21; 31st March 2014 at 04:02 PM.

  9. Thanks to Steve21 from:

    spadam (3rd July 2014)

SHARE:
+ Post New Thread

Similar Threads

  1. Apple Configurator... There's got to be a better way
    By clarnold in forum How do you do....it?
    Replies: 7
    Last Post: 19th December 2013, 08:13 AM
  2. Replies: 7
    Last Post: 8th January 2013, 09:11 PM
  3. greybox, is there an easier way?
    By kal-mc in forum EduGeek Joomla 1.5 Package
    Replies: 3
    Last Post: 14th March 2012, 03:43 PM
  4. Replies: 0
    Last Post: 5th April 2010, 04:57 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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