0

I have this java code and I tried to check by system output the variables if it's all good and I don't get out of while loop, why? I insert alex as username and mihai as password and I don't get out of while loop. And I think it is really strange cuz everything is ok right?

boolean isGoodUandP = false;

String username = "";
String password = "";

while (isGoodUandP == false) {
    System.out.print("Please enter username: ");
    Scanner sUsername = new Scanner(System.in);
    username = sUsername.nextLine();
    System.out.println("username este " + username);

    System.out.print("Please enter password: ");
    Scanner sPassword = new Scanner(System.in);
    password = sPassword.nextLine();
    System.out.println("parola este " + password);

    if ((username == "alex") && (password == "mihai")) {
        isGoodUandP = true;
        System.out.println("Bine ati venit!");
    }
}
deHaar
  • 17,687
  • 10
  • 38
  • 51
Gl0deanR
  • 27
  • 8
  • 2
    Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Feb 05 '19 at 11:45
  • Thank you very much! that helped me! Sorry for duplicating I didn't know that was the problem. – Gl0deanR Feb 05 '19 at 11:47
  • One of the idiosyncrasies of the language – Hovercraft Full Of Eels Feb 05 '19 at 11:54

0 Answers0