dimanche 24 février 2019

if statement always returning false on string comparison in Android Studio [duplicate]

This question already has an answer here:

I have used this code to try and confirm that the user input passwords are the same and there is no mismatch, before proceeding to the next activity. But the if statement always returns a false. It works fine for hardcoded text, but always returns a false for if. Am I missing something?

private void validate(EditText userPassword, EditText userConfirmPassword){
   String userPasswordtext = userPassword.getEditableText().toString();
  String userConfirmPasswordtext = userConfirmPassword.getEditableText().toString();

    if(userPasswordtext == userConfirmPasswordtext){
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }else{
        Toast a=Toast.makeText(this, "Passwords do not match", Toast.LENGTH_SHORT);
        a.show();
    }
}

Aucun commentaire:

Enregistrer un commentaire