Skip to content
Advertisement

Switch statement with Try Catch Exception Java

I’ve got a main menu I’m creating for a program using a switch statement. I’m try to set up Try and Catch Exception so that if a user enters a String rather than an Int, the program will tell the user this is not valid and prompt them to try again.

So far, I have gotten it to tell the user this is not valid whenever this occurs, but it is unable to go back and take an input from the user.

Here is my code:

JavaScript

I have tried adding a do while loop, but I am met with the same issue still. Thank you for reading!

Advertisement

Answer

What you need to do is to use a loop with the correct exit condition. For this specific case, I would recommend using do..while loop. Because this looks like a school assignment, I am not going to give you the full code but a small pseudocode.

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement