How to invert series of denied conditions without affecting logic?

3

Could anyone explain 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 this way it does not 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();
        }
    }
});
    
asked by anonymous 19.12.2016 / 00:33

2 answers

6

Because you failed to invert the relational operator from && to || .

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

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

Whenever you reverse the operators of logical expressions that make up a relational expression, the relational operator must also be inverted to preserve the mathematical truth.

This 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 actually doubt that you need this toString() and I think the rest could be replaced by isEmpty() .

    
19.12.2016 / 01:22
6

They are opposite logics. Try to put in Portuguese that is clearer. 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
    
19.12.2016 / 01:22