'I am trying an else if statement for validation email however it throws an error on else if(password.isEmpty) saying "else without if"
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main);
Tmail = findViewById(R.id.login_email);
Tpass = findViewById(R.id.login_password);
Tconfirmpass = findViewById(R.id.loginRetype_password);
Bar = findViewById(R.id.bar);
button = findViewById(R.id.signin);
mAuth = FirebaseAuth.getInstance();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email, password, confirm;
email = Tmail.getEditText().getText().toString().trim();
password = Tpass.getEditText().getText().toString().trim();
confirm = Tconfirmpass.getEditText().getText().toString().trim();
if (email.isEmpty()){
Tmail.getEditText().setError("Field should not be empty");
Tmail.requestFocus();
}
else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches());{
Tmail.setError("Please enter a valid email Id");
Tmail.requestFocus();
}
else if (password.isEmpty()){
Tpass.setError("Please enter password");
Tpass.requestFocus();
} else if (password.length() < 6) {
Tpass.setError("Password too short");
Tpass.requestFocus();
} else if (!confirm.equals(password)) {
Tconfirmpass.setError("Password mismatch");
Tconfirmpass.requestFocus();
} else {mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "createUserWithEmail:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "createUserWithEmail:failure", task.getException());
Toast.makeText(EmailPasswordActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
updateUI(null);
}
}
});
}
}
});
}
}
Solution 1:[1]
Please format your code properly.
Delete the semicolon in the line else if (!Patterns.EMAIL_ADDRESS.matcher(email).matches());
and you should be good to go.
Solution 2:[2]
the problem is at this line
!Patterns.EMAIL_ADDRESS.matcher(email).matches());
you have added accidentally ; at the end which will terminate the flow, remove ; and try again, hope it will work fine
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Dakshin |
Solution 2 | Sourav Maurya |