My Questions:
I'm trying to handle ArrayIndexOutOfBoundsException in my method. Currently it takes in a string of the user's input and if it matches "^[a-zA-Z]*$" (letters/special characters), then it should keep looping. The array size is [26]. When I input 27, it should catch and handle this exception however it just throws the error.
How could I fix this?
public String checkInput(String userInput) {
try {
while (input.matches("^[a-zA-Z]*$")) {
System.out.println("Please enter a number");
userInput = sc.next();
}
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("The input is invalid, please enter the answer again.");
userInput = sc.next();
}
return userInput;
}
Thanks in advance