Why after such a check is always "you are registered" ? What's wrong?

 if (!((isEmailValid(email) || (isPasswordValid(password)) || (isNameValid(name))))) { Toast.makeText(getActivity(), "Wrong email/password", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getActivity(), "\n" + "You are registered", Toast.LENGTH_SHORT).show(); } private boolean isEmailValid(String email) { return (email.contains("@") && email.length() > 5); } private boolean isNameValid(String name) { return name.length() >= 3; } private boolean isPasswordValid(String password) { return password.length() > 6; } 
  • No, you will not always be "You are registered" . If the email, password and name are both wrong, then there will be a "Wrong email/password" . And why in this text there is not a hint of perhaps the wrong name - I do not understand. Imagine what the user will be if he enters the correct email and password, and he will get the error "Wrong email/password" because of an invalid name. - Regent

3 answers 3

You put in a condition or (||) if at least one of them is true, the expression will be true + negation (!) (It is not clear why). And you need to always have all 3 valid for this, you need an operator and (&&)

 if (isEmailValid(email) && isPasswordValid(password) && isNameValid(name)) { Toast.makeText(getActivity(), "\n" + "You are registered",Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getActivity(), "Wrong email/password",Toast.LENGTH_SHORT).show(); } 

PS To check the E-mail is better to use something like this:

 public static final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile( "^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@" + "((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?" + "[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\." + "([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?" + "[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|" + "([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$"); public boolean isEmailValid(String email) { return EMAIL_ADDRESS_PATTERN.matcher(email).matches(); } 
  • How does this pattern work? - Fedia
  • Regular expression checks email - georgehardcore

According to the rules of mathematical logic,

 !(a || b) == !a && !b 

therefore your condition

 !((isEmailValid(email) || (isPasswordValid(password)) || (isNameValid(name)))) 

equivalent to the following:

 !isEmailValid(email) && !isPasswordValid(password) && !isNameValid(name) 

That is, it only works if you mistakenly entered at the same time e-mail, password and name. Probably, you need to reject the user if he has entered at least one wrong. For this condition must be as follows:

 !isEmailValid(email) || !isPasswordValid(password) || !isNameValid(name) 

This condition will work if at least one parameter is entered incorrectly - e-mail, password or name.

    Well, with brackets you wise, easy to get confused. And it gives out because it gave the following logic: if there is at least one valid field check, then successful registration. And, in theory, the opposite is necessary: ​​if ALL checks are valid, then successful registration. That is, like this: if (! (IsEmailValid (email) && isPasswordValid (password) && isNameValid (name))) {...