This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Equals.IgnoreCase not working" Watch "Equals.IgnoreCase not working" New topic
Author

Equals.IgnoreCase not working

jawann jefferson
Greenhorn

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 = input.next();

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

}

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
Rancher

Joined: May 22, 2012
Posts: 867
    
    5
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
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

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

g="F":
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)

http://www.youtube.com/watch?v=Wrr_k_5QI-g
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11163
    
  16

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
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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
Greenhorn

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Equals.IgnoreCase not working
 
Similar Threads
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?
Incompatible types
help converting my code into classes and arrays