-2

I have this code and my if statement always returns false even though I entered the right inputs.

    boolean z = false;
    String x, y;

    Scanner cin = new Scanner(System.in);

    System.out.println("enter x");
    x = cin.nextLine();

    System.out.println("enter y");
    y = cin.nextLine();

    if(x == "x" || y == "y") {
        z = true;
    }

    System.out.println(z);
    if (z) {
        System.out.println("successful");
    }

1 Answers1

1

You need to use .equals() to compare strings, using == will compare their references.

ex. x.equals("x")

Zarwan
  • 5,537
  • 4
  • 30
  • 48