-3

I am trying to make a noughts-and-crosses prototype. The section of code I am struggling with is getting an user input, and if the input is y, it should change the gamestate to 0 which restarts the game. However this doesn't happen. Can anyone explain why?

Scanner s = new Scanner(System.in);
                System.out.println("Would you like to play again?");
                if (s.next().toLowerCase() == "y") {
                    System.out.println("Okay");
                    g.gamestate = 0;
                } else {
                    g.gamestate = 4;
                }
Steffi Keran Rani J
  • 3,667
  • 4
  • 34
  • 56

2 Answers2

1

Try to use s.next().toLowerCase().equals("y") Operator "==" comparing the links on your string objects, and method "equals" comparing values.

Ihor Dobrovolskyi
  • 1,241
  • 9
  • 19
0

Yes, as somebody commented the solution is need to use equals() instead of ==.

Scanner s = new Scanner(System.in);
  System.out.println("Would you like to play again?");
  if (s.next().equalsIgnoreCase("y")) {
      System.out.println("Okay");
      //g.gamestate = 0;
  } else {
      System.out.println("No");
     // g.gamestate = 4;
  }
Kiran Kumar
  • 1,033
  • 7
  • 20