-2

I wrote this piece of code of simple if statement and the out put is not it should be it gives

What is your Gender? male male The user did not answer the Question.

strong text public static void main(String args[]) {

    Scanner userInput = new Scanner(System.in);
    System.out.println("What is your Gender?");
    String gender = userInput.nextLine();

    System.out.println(gender);

    if (gender == "Male" || gender == "male") {
        System.out.println("Go to Room M1.");
    } else if (gender == "Female" || gender == "female") {
        System.out.println("Go to Room F1.");
    } else {
        System.out.println("The user did not answer the Question.");
    }

    userInput.close();
}
  • there are 10 kinds of people. Those who know binary and those who dont. Nice Name – Jitin Kodian Jan 18 '16 at 11:08
  • you are using `==` and it is a reference comparison not a value one. You should use `equals`, see [this other answer](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) for more info – Paizo Jan 18 '16 at 11:09
  • Just FYI, you can use equalsIgnoreCase() and remove the OR condition – Jitin Kodian Jan 18 '16 at 11:16

1 Answers1

0

use "Male".equals(gender) instead of == . similarly for others

prem kumar
  • 5,641
  • 3
  • 24
  • 36