X-13 Posted March 31, 2014 Posted March 31, 2014 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: http://www.homeandlearn.co.uk/java/images/con_logic/age_exercise_output.gif 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?
TechMonkey Posted March 31, 2014 Posted March 31, 2014 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.
mikeyd101 Posted March 31, 2014 Posted March 31, 2014 You can type convert in java by using the type libraries for ints the type library is Integer so you can use Integer.parseInt() 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
X-13 Posted March 31, 2014 Author Posted March 31, 2014 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.
LosOjos Posted March 31, 2014 Posted March 31, 2014 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. 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.
X-13 Posted March 31, 2014 Author Posted March 31, 2014 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. 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...
tmcd35 Posted March 31, 2014 Posted March 31, 2014 (edited) Long winded, but I think this is the answer the problem is looking for... 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 Edited March 31, 2014 by tmcd35
Steve21 Posted March 31, 2014 Posted March 31, 2014 (edited) Only my personal thoughts, but any reason you don't do it like this? (Note not actual valid java) 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 Edited March 31, 2014 by Steve21 1
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now