1
I am with a basic java problem, I have been researching on logical treatment of strings within an IF condition, but I did not find the answer to my problem:
if (!"y".equals(resposta) || !"n".equals(resposta)) {
System.out.println("REPOSTA INVÁLIDA!");
return;
}
The program asked the user to write if he wants to continue, (yes - y or no - n), so if the answer is DIFFERENT from "y" or "n", he will give the warning inside the if block, but the condition is not working for the string "n", only for the string "y".
Assuming the user presses
n
then he makes the comparison!"y".equals(resposta)
that will betrue
as the operator||
is short circuit for truth the JVM abandons the tests in the firsttrue
because if I continued I would make the comparison!"n".equals(resposta)
that will befalse
and would end withtrue || false
that istrue
given to Truth table of disjunction. How to test if it meets both conditions use a conjunctionif (!"y".equals(resposta) && !"n".equals(resposta))
.– Augusto Vasques
Oh yes, I went for a while without programming and forgot the difference of "or" and "and", thanks for the answer ;D
– Davi Ribeiro