How do I verify the input of the user to match the array in the call method, so that it will return the letter and show it along with validated it as a string?
String[] options1 = { "a", "b", "c" };
choice = getValidString(sIn, "Please enter 'a', 'b' or 'c': ",
"Invalid response. Only the letters 'a', 'b' or 'c' are acceptable.",
options1); // call method
System.out.printf("The letter your entered was: %s\n\n", choice);
public static String getValidString(Scanner sIn, String question,
String warning, String[] choices)
String input = "";
boolean valid= false;
do {
System.out.println(question);
input = sIn.nextLine();
try {
Arrays.asList(choices).contains(input); // this is where the problem resides.
valid = true;
} catch(Exception e) {
System.out.println(warning); }
} while (!valid);
return input ;
}
Desired output:
Please enter 'a', 'b' or 'c': hypotenuse.
Invalid response. Only the letters 'a', 'b' or 'c' are acceptable.
Please enter 'a', 'b' or 'c': b
The letter your entered was: b