1

So I made a program, and I need to keep prompting for coordinates if the user enters a non-negative, invalid input. Meaning that if the user enters a number above 2 or a letter it should ask him to choose again. The problem that I am running into is that right when I enter a letter, the program terminates and I get InputMisMatchException for the letter, and ArrayOutOfBoundsException for the higher numbers. Is there a way to bypass all those errors and just ask the user to pick again?

So for example:

"Enter the coordinates to place an 'X'. Row then Column."
 //enters number > 2 or letter
"Invalid input. Please choose again."
USC23
  • 59
  • 5
  • I've found the solution for entering a letter but I can't seem to get the parameter for a number that is out of bounds that also works with the code I currently have. @Tom – USC23 Jan 23 '16 at 00:40

1 Answers1

0

Use a do/while loop:

boolean valid = false;
do {
    try {
        // "Enter the coordinates to place an 'X'. Row then Column."
        // validate input
        valid = // final value of validation goes here
    } catch (Throwable t) {
        // invalid input
    } while (!valid)
}
hd1
  • 30,506
  • 4
  • 69
  • 81