How to reverse series of denied conditions without affecting logic?

Asked

Viewed 67 times

4

Could someone explain to me why this happens? My apk only works right if I put denial and do something:

public class Main2Activity extends AppCompatActivity {
    private EditText nome, teste, cpf;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        nome = (EditText) findViewById(R.id.editnome);
        teste = (EditText) findViewById(R.id.editcopia);
        cpf = (EditText) findViewById(R.id.editcpf);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!nome.getText().toString().trim().equals("") && !teste.getText().toString().trim().equals("") && !cpf.getText().toString().trim().equals("")) {
                    Toast.makeText(getApplicationContext(), "Não vazio", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), "Vazio", Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}

Now if I put it that way it doesn’t work because?:

button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (nome.getText().toString().trim().equals("") && teste.getText().toString().trim().equals("") && cpf.getText().toString().trim().equals("")) {
            Toast.makeText(getApplicationContext(), "Vazio", Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(getApplicationContext(), "Não vazio", Toast.LENGTH_SHORT).show();
        }
    }
});

2 answers

6

They are opposite logics. Try to make it clear in Portuguese. For example, the first version says:

Se NADA ESTÁ VAZIO:
    Considera preenchido
    // Garantido que TODOS estejam PREENCHIDOS
Senão:
    Considera vazio
    // Se houver PELO MENOS UM VAZIO

The second version says:

Se TUDO ESTÁ VAZIO:
    Considera vazio
    // Somente se TODOS estiverem VAZIOS
Senão:
    Considera preenchido
    // Pelo menos um preenchido, mas pode haver algum vazio

6


Why not reverse the relational operator of && for ||.

In the first you want both to be non-empty. Then check each one if it is not empty, and apply the && to ensure that the two operands are as you wish. The AND requires both to be true.

In the second check if both are empty. But it is not what you want, just one of it is empty to consider that there is something empty. So you need the operator of OR that requires only one of the operands to be true.

Whenever inverting the operators of the logical expressions that make up a relational expression the relational operator has to be inverted as well, to preserve mathematical truth.

So it works as expected:

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    if(nome.getText().toString().trim().equals("") || teste.getText().toString().trim().equals("") || cpf.getText().toString().trim().equals("")){
        Toast.makeText(getApplicationContext(),"Vazio", Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(getApplicationContext(),"Não vazio", Toast.LENGTH_SHORT).show();
    }
}
});

I put in the Github for future reference.

Actually, I doubt you need that toString() and I think the rest could be replaced by isEmpty().

  • 2

    I was going to supplement mine with the ||, now I don’t need

  • Depends he considers an empty space as something in there? the question of isEmpty...

  • @gonz I think not, then I would have to use the trim() same. You need to see if the data needs trim().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.