0

Does anyone know what's wrong with my code, I'm trying to compare the input in my edit text field to ensure their equal value before it creates an account.

     //compare the fields contents
    if(editTextPassword.equals(editTextReEnterPassword)) {
        //they are equal
        //Creating a new user
        firebaseAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()){
                            finish();
                            startActivity(new Intent(getApplicationContext(), Home.class));
                        } else{
                            Toast.makeText(Register.this, "Could not register... please try again", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
    }else {//they are not equal
        Toast.makeText(Register.this, "Passwords do not match... please try again", Toast.LENGTH_SHORT).show();
    }
Caleb Lee
  • 21
  • 2

3 Answers3

2

You have to get the text value of the edit text.

So, really, you can not do editText.equals()

You would do something like this..

String editTextPasswordString = editTextPassword.getText().toString().trim();
String editTextReEnterPasswordString = editTextReEnterPassword.getText().toString().trim();

if(editTextPasswordString.equals(editTextReEnterPasswordString)) {
    //code
}
letsCode
  • 2,774
  • 1
  • 13
  • 37
1

First you need to get the value from EditText using the method getText().toString(), like this:

String newPassword= editTextReEnterPassword.getText().toString();

For more information about the method, please check the follow link: Get Value of EditText

After that, you need to compare the previous string editTextPassword with the new one created, which is the result of the method getText(), editTextReEnterPassword.

So your final code, should be like this:

String newPassword = editTextReEnterPassword.getText().toString();
String atualPassword = editTextPassword.getText().toString();

if(atualPassword.equals(newPassword))
{
    ...
}
else 
{
    ...
} 
Nessie
  • 102
  • 4
0

Try:

if(editTextPassword.getText().toString().equals(editTextReEnterPassword.getText().toString()))

EditText is not a String

  • Thanks for offering your help but when I tried your solution, and run the app it's fine until I create a new account now then the app crash. – Caleb Lee Jul 19 '18 at 07:04
  • This is a different problem. Edit your code with the necessary changes that the answers provided for the EditText comparison problem and present a new question for your current problem. –  Jul 19 '18 at 07:10