• Post Reply Bookmark Topic Watch Topic
  • New Topic

Trying to break out of do loop with Scanner input  RSS feed

 
Joud Kingstone
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a similar question, I want my loop to break and exit the program when the user press e or E(type string), but I using an (integer) variable to switch his choice, so i keep getting error! what can I do:
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Don't add a new question to an old thread. I have split your post off and created a new thread for it. You appear to have pushed the code button twice, but don't worry. I corrected that.

Don't create a new Scanner object inside the loop. Create one Scanner pointing to System.in and give it the widest scope you can. You can keep the same Scanner object for the entire lifetime of your program. If you look at the documentation for Scanner#next(), you will find out what it returns; you can only apply something returning int or Integer to an int variable. Or a few types I am keeping quite about. You can get an int from a Scanner, but you would have to find another nextSomething method.
When you get an int, you are going to find it awkward to test for equality with E. You cannot directly get a char from a Scanner, but if you get a String you can always use charAt(0) to get its first letter as a char. But your exit condition is written wrongly and your loop will never terminate, so you will have to change line 13, too.
 
Tim Brelsford
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think its homework, so here is one way to do it.

 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Brelsford wrote:Think its homework, so here is one way to do it.

Firstly, Tim, a big welcome to the JavaRanch!

You could be right, which is why Sheriff Ritchie provided helpful hints without code. We all try very hard to teach good programming principles here, and therefore encourage members to Do Their Own Homework.

That said, I'm not sure if you were posting the code as a complete solution, or a snippet to help Hope with their solution. I'm a bit concerned about your switch block. Specifically, what will happen if the user presses a key other than 1, 2, 3, 4 - resulting in the default case being triggered?
 
Campbell Ritchie
Marshal
Posts: 56540
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

We usually discourage complete solutions, but I think that one can pass. If you enter something not beginning with 1 2 3 4, then you will exit your loop.
What about this?
System.out.print("Please enter 1 2 3 4: Or 5 to exit: ");
Then you simply read the number.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!