I'm stumped on this - I wrote a try/catch for a helper method. It's purpose is to catch any invalid input (anything that is not "male" or "female" (no specific case). If the input is invalid, it will notify the user and then have them try again. If it is valid, the method will return the input.
When I run the program, it doesn't catch the invalid inputs. Why isn't this working?
Here's the helper Method:
//Helper method that gathers the string input from user
public static String getString() {
//Create new scanner input
Scanner input = new Scanner(System.in);
//Declare and initialize variables
String wordIn = "";
boolean validIn;
//do/while loop for obtaining input and checking validity (try/catch)
do {
validIn = true;
try{
wordIn = input.nextLine();
//End Try
}
catch(Exception invalidInput) {
validIn = false;
input = new Scanner(System.in);
System.out.print("\nYou have entered an invalid input. Please "
+ "enter either \"Male\" or \"Female\" ONLY for this "
+ "selection. The selection is not case sensitive."
+ "\n\nPlease enter your selection: ");
//End Catch
}
//While input is valid, return the input
} while (!validIn);
return wordIn;
//End helper
}
Here's the code from the test:
//Obtain user input and print output
String passGender = getString();
System.out.println("\n" + titanic.getSurvivedPassengersGender(passGender)
+ " " + passGender.toLowerCase() + " passengers survived the "
+ "sinking of the Titanic.");
It seems as if I don't have a conditional set right...I can't find where I went wrong. I'm still new to this, so I'm sure it's a simple mistake. Any help is greatly appreciated. Thank you folks!