1
I have a code where I need to verify that two entries are true.
I’m doing like this:
if (!estudante.getOutTotal.equals("0") && !estudante.getOutEmAtendimento.equals.("0")) {
// Faz algo...
itens.add(estudante);
}
If the totals of emAtendimento and Outematendimento are values "0" do not want to add in my list now in this situation could occur that outEmAtendimento is "0" but outTotal is "50"... in this case it would be necessary to add in the list but this logic does not do it.
I did it:
if (!estudante.getOutTotal.equals("0") && !estudante.getOutEmAtendimento.equals.("0")) {
// Faz algo...
itens.add(estudante);
} else if (!estudante.getOutTotal.equals("0") || !estudante.getOutEmAtendimento.equals.("0")) {
// Faz algo...
itens.add(estudante);
}
So not to delete from all I check if either one or the other is not zero if not added to the list.
Is that right? Is there any better way?
At first I gave a basic improvement and it worked as I expected.
Follows:
if (!estudante1.equals("0") && !estudante2.equals("0")) {
itens.add(estudante1);
itens.add(estudante2);
System.out.println("1 if");
} else if (!estudante1.equals("0") || !estudante2.equals("0")) {
itens.add(estudante1);
System.out.println("2 if");
itens.add(estudante2);
}
Aline, tip from those who have suffered a lot in this Java life: use the constants on the left side of
equals
. Type,"0".equals(estudante.getOutTotal())
. This may not be your case, but in general this avoids Npes. Another point I found strange, when you put the name of the method with the endTotal
i hope to have a numerical value; if this is true, compare a number with aString
returns false, since they are elements of different classes; reiterating: I found it strange, but it depends on more context to state if it is correct– Jefferson Quesado
you can do the following, if I understand correctly, you have two cases, where if both are true you want to do nothing, but if one of them is true and the other is false, you want to do something, then you could put a more comprehensive if in the getOutTotal case, If it were true (value 0) you would put 2 more ifs inside it, one to check if getOutEmAtendimento is true and another to check if it is false. I believe that so you cover all possibilities
– Danilo
What @Jeffersonquesado commented is called Yoda condition, if you want to know more, see
– MarceloBoni
I found your paragraph talking about the conditions confusing. In it you cite the value 50, but there is nowhere in the example code mention to 50. It is also late where I am, sleep does not let me try to interpret meanings
– Jefferson Quesado
Your logic doesn’t make much sense, think like this:
Se(condicaoTal) Faça { Escreva "Oi" } Senão { Escreva "Oi" }
Either way will do the same thingEscreva "Oi"
, this could be optimized only withSe(condicaoTal) Faça { Escreva "Oi" }
, for now neither bang coming into question of the question itself, only the first strange thing I saw– MarceloBoni
@Marceloboni , I thought it was just good practice. I’ll even share your comment talking about the Yoda comparison there at work
– Jefferson Quesado
Wait, I’ll open the eclipse and write it right. Just a second.
– Aline
At first, as if
getOutEmAtendimento
ORgetOutTotal
is different from zero, the action is the same:itens.add(estudante);
, the firstif
is unnecessary. People are seeing several possible issues in your code, so ideally you should include other information, for example, the type of variablegetOutTotal
, or the rest of your class... ;)– Daniel
Guys! It worked!
– Aline
Right! I’ll review it again wait...
– Aline
I’m glad to have helped, I just don’t know with what yet... in the code you put after editing, it suffered more direct to the subject, more organized, but I still did not identify where or why the correction happened
– Jefferson Quesado
Jefferson Quesado, I’ll have to parse. That’s how they work there......
– Aline
Boy is so in case return zeroed values I will not add in my listview to be displayed there 0, I prefer not to add in listview...sorry there anything...
– Aline
I’m making like a report for android. : 3
– Aline
Marceloboni realized that now I thought the symbol && made exclusion if they were two different values... for example student 1 = 0 but student = 34. so I put so with two ifs...opa hold but do. Just put one || that solves the problem. And I delete the first if. Ah understood. but I’m sleepy I’m getting in the way...
– Aline
I thought the || operator excluded from 'processing' if they were two true positions...
– Aline