-1

Please can someone help? Im not sure why this if statement wont work, does anyone have any idea?

all i want it to do is if the username and password match close the current gui window and open another.

At the mo when the username and password are correct it still says incorrect.

Im sure its something really silly.

    String username = usernameField.getText();
    char[] password = passwordField.getPassword();
    username = username.toLowerCase();
    String passwordString = new String (password);
    passwordString = passwordString.toLowerCase();
    String loginUsername = "cj410";
    String loginPassword = "email";
    if (username == loginUsername && passwordString == loginPassword){
        dispose();
        homePage hp = new homePage();
        hp.setVisible(true);
    } else {
        LoginError le = new LoginError();
       le.setVisible(true);
    }
Joe Cobain
  • 91
  • 1
  • 9
  • In Java/Android, == is for primitive comparison, equals() is for object comparison. – kosa Jan 16 '15 at 16:31

1 Answers1

1

You are using == which is reference equality. You need to call equals() instead.

if(loginUsername.equals(username) && loginPassword.equals(passwordString ) {
   // etc
}

Compare using it as above, because if username or passwordString are null, it will throw a NullPointerException if you compare them the other way around.

Todd
  • 30,472
  • 11
  • 81
  • 89