I am trying to accept input from the user in the form of a character. I have that working but I need to check and make sure it is one of 6 characters (H, h, S, s, L, l). I have a while loop but as soon as add more than one character statement to it, the loop gives the error for every value that should be correct.
Here is the function:
private static char getHighLow(Scanner keyboard)
{
System.out.println("High, Low, or Sevens (H/L/S): ");
String choiceString = keyboard.next();
char choice = choiceString.charAt(0);
while (choice != 'H' || choice != 'h' || choice != 'L' || choice != 'l' || choice != 'S' || choice != 's')
{
System.out.println("You have entered an invalid entry.");
System.out.println("High, Low, or Sevens (H/L/S): ");
choiceString = keyboard.next();
}
return choice;
}
What is the best way to continue checking for multiple characters like this?
There is an error in your logic: you should be connecting those tests with AND:
&&:Furthermore
choiceis never being updated within the body of the loop, so it will never change. Duplicate thechoice = choiceString.charAt(0);after you re-readchoiceStringwithin the loop.Conceptually, you want to keep asking for input so long as the character isn’t H AND isn’t L AND isn’t S.