Java: Checking contents of char variable with if condition
Posted
by Troy
on Stack Overflow
See other posts from Stack Overflow
or by Troy
Published on 2010-02-13T07:04:55Z
Indexed on
2010/05/09
5:18 UTC
Read the original article
Hit count: 243
Hello, I have a char variable that is supposed to contain either a Y,y,n or N character, I want to test if it does not contain it, then display an error message and exit the program.
This is the code I am using;
if (userDecision != 'Y' || userDecision != 'y' || userDecision != 'n' || userDecision != 'N')
{
System.out.println("Error: invalid input entered for the interstate question");
System.exit(0);
}
Irregardless of what is in the variable it always returns true and executes the command to exit the program, what am I doing wrong?
© Stack Overflow or respective owner