Doubt about logic, ifs with multiple checks

Asked

Viewed 74 times

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);

}
  • 2

    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 end Total i hope to have a numerical value; if this is true, compare a number with a String 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

  • 1

    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

  • 2

    What @Jeffersonquesado commented is called Yoda condition, if you want to know more, see

  • 1

    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

  • 1

    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 thing Escreva "Oi", this could be optimized only with Se(condicaoTal) Faça { Escreva "Oi" }, for now neither bang coming into question of the question itself, only the first strange thing I saw

  • 1

    @Marceloboni , I thought it was just good practice. I’ll even share your comment talking about the Yoda comparison there at work

  • 2

    Wait, I’ll open the eclipse and write it right. Just a second.

  • 1

    At first, as if getOutEmAtendimento OR getOutTotal is different from zero, the action is the same: itens.add(estudante);, the first if is unnecessary. People are seeing several possible issues in your code, so ideally you should include other information, for example, the type of variable getOutTotal, or the rest of your class... ;)

  • Guys! It worked!

  • Right! I’ll review it again wait...

  • 1

    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, I’ll have to parse. That’s how they work there......

  • 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...

  • 1

    I’m making like a report for android. : 3

  • 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...

  • I thought the || operator excluded from 'processing' if they were two true positions...

Show 11 more comments

1 answer

1


My opinion. Briefly, that would be enough:

if (!"0".equals(estudante1.getOutTotal())){
  itens.add(estudante1);
}
if (!"0".equals(estudante2.getOutTotal())){
  itens.add(estudante2);
}

And if only one of the conditions were enough to be added to the two listings:

if (!"0".equals(estudante1.getOutTotal())){
  itens.add(estudante1);
  itens.add(estudante2);
}
if (!"0".equals(estudante2.getOutTotal())){
  itens.add(estudante1);
  itens.add(estudante2);
}
  • blz boy.... Thanks a lot.

Browser other questions tagged

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