0

I'm trying to verify if the input of the user is M or F (discarding every other letter), through the != operator. I know this operator doesn't work, so I tried this:

System.out.println("Whats the gender?");
gender = read.next();
while ( !"M".equals(gender) || !"F".equals(gender))
      System.out.println("Rewrite your gender");
      gender = read.next();
}

however it doesn't verify. Doesn't matter what letter I input, it will always say to rewrite.

GBlodgett
  • 12,704
  • 4
  • 31
  • 45
Diogo Teixeira
  • 91
  • 1
  • 12
  • 1
    Say you input `F`. Is it not `M`? Likewise if you input `M` is it not `F`? – GBlodgett Dec 11 '19 at 23:57
  • @GBlodgett my goal is: if the user inputs F or M, the program goes on, but if the user inputs any other string that is not F or M, the program asks them to rewrite the gender. I guess now my doud is clear. – Diogo Teixeira Dec 12 '19 at 00:08
  • I understand your goal; I'm trying to get you to see the logic error in your `while` condition – GBlodgett Dec 12 '19 at 00:09
  • Try with this condition : while ( !("M".equals(gender) || "F".equals(gender))) – Neeraj Dec 12 '19 at 04:08

1 Answers1

3

The condition must be !"M".equals(gender) && !"F".equals(gender)

Pablo DbSys
  • 532
  • 1
  • 7
  • 17