I have a class I wrote in Java and one of the methods is getCommand() The purpose of this method is to read in a string and see what the user typed in matches any of the acceptable commands.
This is how I wrote it initially:
public char getCommand(){
System.out.println("Input command: ");
command = input.nextLine();
while(command.length() != 1){
System.out.println("Please re-enter input as one character: ");
command = input.nextLine();
}
while( command.substring(0) != "e" ||
command.substring(0) != "c" ||
command.substring(0) != "s" ||
command.substring(0) != "r" ||
command.substring(0) != "l" ||
command.substring(0) != "u" ||
command.substring(0) != "d" ||
command.substring(0) != "k" ||
command.substring(0) != "f" ||
command.substring(0) != "t" ||
command.substring(0) != "p" ||
command.substring(0) != "m" ||
command.substring(0) != "q"){
System.out.println("Please enter a valid character: ");
command = input.nextLine();
}
fCommand = command.charAt(0);
return fCommand;
}
Now, I see the problem with this is that since I use the OR operator, it won't escape that loop because the character I type in will always not equal one of them. I tried changing it to the AND operator, but same problem. What would be the best way to only accept those specific characters? Much appreciated.