File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Equals.IgnoreCase not working

 
jawann jefferson
Greenhorn
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 972
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic