I have an assignment that I need to validate that an input (String) is valid and if not throw an exception. When I have my there is a menu option (1-6) and when they select 2, which is to enter a new monkey it will skip the user entering input and jump straight to the error. If i move my variable after the try{ then it still doesnt allow any input and has an error because this variable is needed lower down. Any help of how to get this to work would be great. Thanks
public static void intakeNewMonkey(Scanner scanner) {
System.out.println("What is the monkey's name?");
String monkeyName = scanner.nextLine();
try{
if(!monkeyName.matches("^[a-zA-Z]+$")){
throw new Exception("is wrong input!");
}else{
System.out.println("perfect!");
}
}catch(Exception e){
System.out.println(e.toString());
}