简体   繁体   中英

Comparing a string AND and OR at the same time

I have a problem to compare string by using AND and OR. I would like to display an error if the comment is empty AND "rule" equals "A" OR "B" OR "C". That's means, if "rule" equals to anything else than "A", "B" or "C" and "comment" is empty, it's fine, but in other case, it should display an error.

At this time, the && only work with equals.("C") and isEmpty. I'm not able to make it works with "A" and "B" at the same time...

The result is if equals A and comment is NOT empty, the error still appears, same for B and works fine for C...

    String comment = commentet.getText().toString();
    if (rule.equals("A") || rule.equals("B") || rule.equals("C") && TextUtils.isEmpty(commentaire)) {
        commentet.setError("Comment is necessary");
        return;
    }

Do you know how to solve this problem ?

Thank's for your help.

Does this work :

    String comment = commentet.getText().toString();
    if ((rule.equals("A") || rule.equals("B") || rule.equals("C")) && TextUtils.isEmpty(commentaire)) {
        commentet.setError("Comment is necessary");
        return;
    }

Basically put your A,B,C checks inside a parenthesis.

您可以将regex用于或部分:

if(TextUtils.isEmpty(commentaire) && rule.matches("A|B|C")){ ... }

try this,

String comment = commentet.getText().toString();

if ((rule.equals("A") || rule.equals("B") || rule.equals("C")) && TextUtils.isEmpty(commentaire)) {

commentet.setError("Comment is necessary");

return;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM