aspose file tools*
The moose likes Beginning Java and the fly likes Break out of a switch inside a loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Break out of a switch inside a loop" Watch "Break out of a switch inside a loop" New topic
Author

Break out of a switch inside a loop

Maria Kowalczyk
Greenhorn

Joined: Jul 03, 2013
Posts: 15
    
    1
Hallo Ranchers!
I have a problem with a program I write.
I'll post simplified example.I don't know what I am doing wrong. The program is supposed to take a line from user and compare it against case statements in swicth.
If it matches it should print appropiate text and ask again, and if not the program should exit (printing a message)

The output I get:

Enter a number: 1
one
two
Enter a number: 2
two
Enter a number: 3
Enter a number: 3
Enter a number: Exception in thread "main"


The exception message happens when I press Ctrl + C to exit the program.

The output I want:

Enter a number: 1
one
Enter a number: 2
two
Enter a number: 3
Exiting...


So, any help please


Something is no yes.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Maria Kowalczyk wrote:So, any help please

The problem is thatdoesn't break out of the while loop or exit the method; it breaks out of the switch statement.

What do you think you might use to do the first?

If you get completely stuck, I'll give you some more help, but try and work it out for yourself first.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Maria Kowalczyk
Greenhorn

Joined: Jul 03, 2013
Posts: 15
    
    1
I did it

I introduced a variable to control the loop. And you saying it breaks out of the switch statement helped me with aother issue .
Thanks for help . Buziak :*
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Maria Kowalczyk wrote:I did it
Well done!

I introduced a variable to control the loop. And you saying it breaks out of the switch statement helped me with aother issue .

Great. Now that you have worked it out, FYI: a slightly simpler way with your original loop would simply be:I'll leave you to work out why.

Thanks for help . Buziak :*

You're welcome. Don't get many buziaky at my age.

Winston
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11499
    
  16

your original post has:

a simplified example of your code
What you want it to do
What it really does
What you expect it to do.

That's pretty much a perfect post of how questions should be asked.

Have a cow!!!


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Maria Kowalczyk
Greenhorn

Joined: Jul 03, 2013
Posts: 15
    
    1
Winston Gutkowski wrote:Great. Now that you have worked it out, FYI: a slightly simpler way with your original loop would simply be:I'll leave you to work out why.
That's also a good idea . But it won't work in my case (in the real code) as my function does some stuff after the loop.

fred rosenberger wrote:your original post has:

a simplified example of your code
What you want it to do
What it really does
What you expect it to do.

That's pretty much a perfect post of how questions should be asked.

Well, I've read HowToAskQuestionsOnJavaRanch .

fred rosenberger wrote:Have a cow!!!

Thanks!!! Buziak for you too :*
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8427
    
  23

Maria Kowalczyk wrote:
fred rosenberger wrote:That's pretty much a perfect post of how questions should be asked.
Well, I've read HowToAskQuestionsOnJavaRanch .

But you'd be surprised how many don't.

So thanks from me too...and keep it up.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Break out of a switch inside a loop