Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Break out of a switch inside a loop

 
Maria Kowalczyk
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Winston Gutkowski
Bartender
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Maria Kowalczyk
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12124
30
Chrome Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!!
 
Maria Kowalczyk
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10417
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic