my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Having trouble with switch, case,break, Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Having trouble with switch, case,break, " Watch "Having trouble with switch, case,break, " New topic
Author

Having trouble with switch, case,break,

Angela Li
Greenhorn

Joined: Nov 04, 2011
Posts: 12
I'm suppose to write a class that asks for an int between one and five inclusive, then check if the int is really in the range, then use-- switch, case, break--to generate an appropriate message that corresponds with the number entered. And last return to the ask for int step.
This is what I have did so far but I am having trouble when I input an int inside the accepted range, it gives me the default.

Timothy Oldbean
Greenhorn

Joined: Jun 23, 2011
Posts: 17
Hi Angela I'm new here too so I'm not a lot of help yet. You can make the code a lot more readable by using the "Code" button.

I'm pretty sure someone will come along with an answer to your question soon :-)



If it wasn't for physics and law enforcement, I'd be unstoppable!
Angela Li
Greenhorn

Joined: Nov 04, 2011
Posts: 12
Thanks for the code suggestion, I appreciate it.
Aniket S. Kulkarni
Ranch Hand

Joined: Jun 15, 2011
Posts: 87

@Angela Li: Why are you complicating the code?
The code is fine but...
Just change the return type of getChoice() method to int and replace the line no. 43 with
return keybd.nextInt() ;


Aniket Kulkarni
Oracle Certified Professional, Java SE 6 Programmer.
Angela Li
Greenhorn

Joined: Nov 04, 2011
Posts: 12
Thanks Aniket, that was a stupid of me.
Aniket S. Kulkarni
Ranch Hand

Joined: Jun 15, 2011
Posts: 87

My pleasure!!!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38087
    
  22
Why have you got the same line endNow = true; in six different places? That means your loop is invalid; you can only iterate it once.
D. Ogranos
Ranch Hand

Joined: Feb 02, 2009
Posts: 214
You can also check for char constants in the switch statement. Your getChoice() method returns a char, so simply check for that:



Note the single quotes around the numbers.
 
 
subject: Having trouble with switch, case,break,
 
Similar Threads
trouble using .useDelimiter
Do while error, I'm going crazy
read an expression and solve it...
Writing a loop statement to stop at 3 and inserting a break to quit loop
no such element help