File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Equals.IgnoreCase not working Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Equals.IgnoreCase not working" Watch "Equals.IgnoreCase not working" New topic

Equals.IgnoreCase not working

jawann jefferson

Joined: Mar 06, 2012
Posts: 6
I'm having an issue with the equals.IgnoreCase() method in comparing strings. Heres the code:

System.out.println("\nPlease enter gender. 'M' for male and 'F' for female: ");
gender =;

while(!gender.equalsIgnoreCase("M") || !gender.equalsIgnoreCase("F"))
System.out.println("\nInvalid gender. Please re-enter: ");
gender =;


Its supposed to compare the the gender input to M or F and while it is neither of them ask the user to re-enter. It seems like it should work with the ! operator out front saying if gender is not equal to M or not equal to F then check. Problem i am getting is no matter what i enter it says it is invalid and stays in the loop forever.
Ivan Jozsef Balazs

Joined: May 22, 2012
Posts: 865
What is the variable "input"? How does its "next" work?

Anyway: is by any chance some trailing (new line or the like) at the end?
Jeff Verdegan

Joined: Jan 03, 2004
Posts: 6109

This: while(!gender.equalsIgnoreCase("M") || !gender.equalsIgnoreCase("F")) is always true.

g != "M" -- false
g != "T" -- true
false || true --> true

g != "M" -- true
g != "F" -- false
true || false --> true

g = "X"
g != "M" -- true
g != "F" -- true
true || true --> true

The only way for that to be false if is g != "M" is false (meaning g is "M") and g != "F" is also false at the same time (meaning g is "F"). In other words, g has be both M and F and the same time for it to be false.

DeMorgan's laws:

!A || !B == !(A && B)
!A && !B == !(A || B)
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 10911

Your condition is wrong.

Try working out what everything evaluates to. I assume you the equalsIgnoreCase DOES work just fine.

If you can't work it out on paper, split out your conditions...something like

Boolean isNotMale = !gender.equalsIgnoreCase("M");
Boonean isNotFemale = !gender.equalsIgnoreCase("F");

and then do

while (isNotMale || isNotFemale)

and see what happens

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan

Joined: Jan 03, 2004
Posts: 6109

fred rosenberger wrote:

while (isNotMale || isNotFemale)

and see what happens

And print out isNotMale and isNotFemale while you're at it, in case there's any doubt left.
jawann jefferson

Joined: Mar 06, 2012
Posts: 6
Thanks for the great help. I did have to take another look at my condition logic. Instead of || i had to use &&. My conditions were constantly showing as true for the OR statement.
thanks a lot
It is sorta covered in the JavaRanch Style Guide.
subject: Equals.IgnoreCase not working
Similar Threads
help converting my code into classes and arrays
Incompatible types
How do I turn this object into an array....
Testing a string from a scanner input in an if/else statement
Nested while loop inside do...while loop?