Kaptain
Kaptain

Reputation: 53

JAVA looping logic error with NOT .equalsIgnoreCase()

I'm trying to use equalsIgnoreCase() in a while loop to try and check if something other than what was intended to be written was written by using the NOT (!) operator. For example:

String temp="A";
boolean x =(!temp.equalsIgnoreCase("a")) ;

See, this works with a while loop. If it's not A, it will keep looping but this next line does not

boolean x =(!temp.equalsIgnoreCase("a") || !temp.equalsIgnoreCase("b")) ;

This does not seem to work anymore. This returns true, no matter what you type, even if it is a or b. So when I use the whole line of code to check for any of the letters that are not suppose to be used:

while (!temp.equalsIgnoreCase("A") || !(temp.equalsIgnoreCase("B")) ||!temp.equalsIgnoreCase("D")|| !temp.equalsIgnoreCase("P") || !temp.equalsIgnoreCase("S"))
{ ***Do Code***}

it loops whatever you put in, even if it will equal one of the letters.

When there is more than one !temp.equalsIngnoreCase, the code does't work with OR (||).

The only way I can get it to work is if I change the OR to AND

while (!temp.equalsIgnoreCase("A") && !(temp.equalsIgnoreCase("B")) && !temp.equalsIgnoreCase("D")&& !temp.equalsIgnoreCase("P") && !temp.equalsIgnoreCase("S"))

Even though I seem to have found a solution, I still don't understand why OR doesn't work but AND does. I removed the NOT to see if everything works, and it seems to loop perfectly when one of the letters is entered.

Upvotes: 1

Views: 22977

Answers (4)

Rohit Jain
Rohit Jain

Reputation: 213311

I still don't understand why OR doesn't work but AND does

The expression using || will always be true at any given value of temp. Because, temp cannot be both a and b at the same time. If it is a, then the 2nd part of || will be true, and if it is equal to b or any other value, the first part will be true, thus making the entire expression true in both the cases.

With &&, your while will only be true, if temp is neither of a nor b.

Alternatively, if you are going to test temp against many values, you can change your while condition to look simpler:

while (!"ABDPS".contains(temp.toUpperCase())) {

}

Upvotes: 2

Sorontur
Sorontur

Reputation: 510

This is all about logic.

A OR B means that is is true when A is true or B is true or both are true.

In your special case it is only possible that one of your equalsIgnorecase() can ever work, so you wrote something like a tautology which means an endless loop. You can read about boole algebra here: http://en.wikipedia.org/wiki/Boolean_algebra_%28structure%29

Kind of some theory but it explains what you need to know when you write boolean expressions. Hope this helps :)

Upvotes: 0

Ankit
Ankit

Reputation: 6622

its a foul logic. the code

(!temp.equalsIgnoreCase("A") || !(temp.equalsIgnoreCase("B")) ||!temp.equalsIgnoreCase("D")|| !temp.equalsIgnoreCase("P") || !temp.equalsIgnoreCase("S"))

means

if char is not A, or not B, or not D, or not P, or not S. It will always evaluate to true, since is char is A, it will neither be B,D,S nor P. so is for the others.

if you want it to be OR logic, it should be:

(!(temp.equalsIgnoreCase("A") || (temp.equalsIgnoreCase("B")) ||temp.equalsIgnoreCase("D")|| temp.equalsIgnoreCase("P") || temp.equalsIgnoreCase("S")))

which means, not when the char is either of A, B, D,S or P

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502106

it loops whatever you put in, even if it will equal one of the letters.

Yes, of course it does.

You're asking it to keep going while it isn't A or it isn't B. Well nothing can be both A and B... if the value is equal to B then it won't be equal to A so the first operand will keep the loop going. If the value is equal to A then it won't be equal to B so the second operand will keep the loop going.

Your solution of changing to AND is correct - you want the value to not be A and not be B (i.e. it's neither A nor B).

Alternatively, you could use OR internally, but put a NOT around the whole thing:

while (! (temp.equalsIgnoredCase("A") || temp.equalsIgnoreCase("B") || ...))

Upvotes: 3

Related Questions