Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java Java Basics Perfecting the Prototype Censoring Words - Looping Until the Value Passes

Problem with boolean variable

Hi guys, I am french but don't worry. I tried to create a program where the client has to input his gender. I created something like If the gender is not equal to male(homme) "OR" female(femme), the answer is wrong. But when I put in the console female or male the computer don't recognize them. Saying that there are both wrong. WTF ??

It look pretty much like this :

 String gender;
  boolean fakeAnswer; 

  do { gender = console.readLine("gender?:\n  ");
          fakeAnswer = (!gender.equalsIgnoreCase("homme")) || (!gender.equalsIgnoreCase("femme"));
  if (fakeAnswer) { console.printf("what's your gender?\n"); } }

  while(fakeAnswer);

4 Answers

Joe Rowe
Joe Rowe
2,225 Points

Bonjour!

Consider your If Statement...

You state that the answer is fake IF the gender is not homme OR the gender is not femme.

Now, suppose my gender is homme. As far as your program is concerned, it is true that my gender is not femme!

Can you see where the error lies?

How else could you ask this question? You will need to use a different word than OR

Bonjour joe, Thank you for your Help my dear friend !

The problem is that I thought "Or" meant that : if the answer is not homme or femme it's wrong. So All the other words, except for "homme or femme", are false

and "&&" can't work because nobody can be Homme and Femme in the same time... I don't get it.

Joe Rowe
Joe Rowe
2,225 Points

Think again about &&.

If someone is not homme AND they are not femme, should fakeAnswer be true or false?

I joe, I think I get what you are saying.

if I use OR, homme will not be Femme and vice versa.

So I should use "&&" So both the hypotheses are taken into account.

Thank you so much my friend :)

Joe Rowe
Joe Rowe
2,225 Points

Youve got it!

This is a "logic error" whereby the code works perfectly, but it doesn't do what you expected it to!

These are much harder to debug a lot of the time as you have to question your own expectations and what result you are looking for.

Sometimes a good approach is to try asking the question a different way, or writing it down on paper. Many times the error quickly becomes clear!