I am trying to find a way to give some constraints as to what a user can input. More specifically, the input has to respect certain limits within my program. also, I want to continue asking the user for a valid input as long as he does not give an integer respecting the limits. I think my main point is to catch the InputMismatchException (if the user inputs a string for example) and to re-ask for an input if ever the integer inputs are not within the limits. I am confused as to how to use the try catch method. Any help would be appreciated! thank you.
Scanner sc = new Scanner(System.in);
System.out.println("Please enter your next move: ");
int rowNum = sc.nextInt();
int columnNum = sc.nextInt();
try {
while (((rowNum < 0)||(columnNum < 0)) || ((rowNum >= Board.length)||(columnNum >= Board[0].length))) {
System.out.println("Your move is not valid, Please enter a valid move");
rowNum = sc.nextInt();
columnNum = sc.nextInt();
}
while (Board[rowNum][columnNum] != ' '){
System.out.println("Your move is not valid, Please enter a valid move");
rowNum = sc.nextInt();
columnNum = sc.nextInt();
}
} catch(Exception e) {
System.out.println("Your move is not valid, please enter a valid move");
sc.nextInt();
}