In your case, you have four admissible inputs namely "A", "B", "C", "D". This can easily be implemented only using boolean operators; namely the loop condition is when your input is neither that of "A" nor "B" nor "C" nor "D".
Scanner input = new Scanner(System.in);
System.out.println("Enter a selection (NOR): ");
String string1 = input.next();
while ( !string1.equals("A")
&& !string1.equals("B")
&& !string1.equals("C")
&& !string1.equals("D")) {
System.out.println("Enter again (NOR): ");
string1 = input.next();
}
input.close();
But what about situations where there are arbitrarily but finitely many admissible inputs, such as having all letters of the Hungarian alphabet? The set may be the best option.
Scanner input = new Scanner(System.in);
// Either manually or use a method to add admissible values to the set
Set<String> admissible = new HashSet<String>();
admissible.add("A");
admissible.add("B");
admissible.add("C");
admissible.add("D");
// ... and so on. You can also use helper methods to generate the list of admissibles
// in case if it is in a file (such as a dictionary or list of words).
System.out.println("Enter a selection (Set): ");
String string1 = input.next();
// If input string1 is not found anywhere in the set
while (!admissible.contains(string1)) {
System.out.println("Enter again (Set): ");
string1 = input.next();
}
input.close();
set.contains(string1)in while loopdo while loop