I can not figure out how to write the condition. I have 3 editText, say 1, 2, 3

So if 1 or 2 is filled then 3 does not need to be checked if 1 or 2 is empty then you need

That's what happened to me

boolean isOneExist = !TextUtils.isEmpty(one); boolean isTwoExist = !TextUtils.isEmpty(two); if (!isOneExist || !isTwoExist) { //check editText 3 } 

As a rule, text is entered in 1 or 2 and it turns out that always one value turns out true is another false and I don’t understand how to do it correctly

It seems to be just

  • Но всегда одно значение получается true другое false - give an example. - post_zeew
  • @post_zeew added a question - Aleksey Timoshchenko
  • one
    1 or 2 is filled, then 3 is not necessary to check, if 1 or 2 is empty, then you need to understand that when one of the two is filled, then so is one of the two empty? Therefore, your condition is only true when both are filled - MihailPw
  • @ AGS17 no, 1 or 2 is filled, then it is not necessary, if 1 and 2 is empty, then it is necessary ... The code does not enter the if block only if the field is filled in and 1 and 2, but this is not what I need as you understand .. I need one filled floor (either 1 or 2) if block if not executed - Aleksey Timoshchenko
  • @ AGS17 or did I understand you wrong? - Aleksey Timoshchenko

3 answers 3

When using the logical OR ( || ) it turns out that the condition is fulfilled when at least one field is empty (that is, either the first, or the second, or both).

To check the third field only in the case when both first fields are empty, you need to use AND ( && ):

 if (!isOneExist && !isTwoExist) { ... } 

You can also get rid of double negation:

 boolean isOneEmpty = TextUtils.isEmpty(one); boolean isTwoEmpty = TextUtils.isEmpty(two); if (isOneEmpty && isTwoEmpty) { ... } 

    1 or 2 is filled then 3 does not need to be checked, if 1 or 2 is empty - then it is necessary

    It cannot be that one of them will be filled, but it will not be empty.

    For clarity, your text boxes can be:

      | пустой | заполненный TextBox 1 | + | - TextBox 2 | + | - --------------------------------- TextBox 1 | + | - TextBox 2 | - | + --------------------------------- TextBox 1 | - | + TextBox 2 | + | - --------------------------------- TextBox 1 | - | + TextBox 2 | - | + 

    You ask it:

      | пустой | заполненный TextBox 1 | - | + TextBox 2 | - | - --------------------------------- TextBox 1 | - | - TextBox 2 | - | + 

    We will analyze your condition:

    1 or 2 is full then 3 does not need to be checked

    If 1 or 2 is full, then 2 or 1 will be empty, respectively.

    if 1 or 2 is empty - you need

    If 1 or 2 is empty, then 2 or 1 will be filled accordingly.

    UPD:

    According to the commentary , you still need to check for 2 empty fields. Accordingly, I can offer to improve the readability of the code:

     boolean isFirstEmpty = TextUtils.isEmpty(one); boolean isSecondEmpty = TextUtils.isEmpty(two); if (isFirstEmpty && isSecondEmpty ) { //check editText 3 } 
    • in the comment already "1 or 2 is filled, then it is not necessary, if 1 and 2 are empty, then it is necessary" - pavlofff
    • one
      @pavlofff I expected that the question would be the correct condition. - MihailPw

    I would do this:

      boolean empty = TextUtils.isEmpty(one); empty &= TextUtils.isEmpty(two); if (empty) { //check editText 3 }