0

I have a small problem where even the output shows that condition is met but in the if-then-else the else block is executed.

import java.util.Scanner;
class temp {
  public static String name;
  public static String gender;
  public static String type;

  public static void main (String args[]) {

  Scanner inp = new Scanner(System.in);

  //Get Input
  System.out.print("Are you male or female (M/F): ");
  gender = inp.next();
  System.out.println("");

  //Check if it is valid option
  if (gender == "M" || gender == "m") {
    gender = "Male";
  } else if (gender == "F" || gender == "f") {
    gender = "Female";
  } else {
    System.out.println("Incorrect keyword " + gender + " detected. Please try again");
    System.out.print("Are you male or female (M/F): ");
    gender = inp.next();
    System.out.println("");
  }

  //Print Output
  System.out.println(gender);
  }

}

I am beginner so any help will be appriciated P.S: Sorry for wrong indents

UbadahJ
  • 46
  • 6

0 Answers0