Make a chain of if and pick up a value for a variable when it meets the conditions

0

What happens is that I made a chain of if () with many conditions and I hope to collect a different value for "TipoAtencionX" depending on each of the conditions.

Example: If you meet condition 1, I want "TypeAttendanceX" to be "attention1". If you do NOT fulfill condition 1 and yes with condition 2, I want "TypeAttentionX" to be "attention2". If you do NOT fulfill condition 1 and 2, but do with condition 3, I want "TypeAtentionX" to be "attention3", etc.

This is my code, the problem is that it always works only until the first if and when it does not comply with condition 1 it is still working with that data and the idea is that if it does not comply, avoid it or ignore it and follow the chain of if ().

This is my code

if (TipoAtencion1.equals("no contiene")){
    TipoAtencionX  = "TipoAtencion1";
} else if (!TipoAtencion1.equals("no contiene") && TipoAtencion2.equals("no contiene")){
    TipoAtencionX = "TipoAtencion2";
} else if (!TipoAtencion1.equals("no contiene") && !TipoAtencion2.equals("no contiene") &&
    TipoAtencion3.equals("no contiene")){
    TipoAtencionX = "TipoAtencion3";
} else if (!TipoAtencion1.equals("no contiene") && !TipoAtencion2.equals("no contiene") &&
    !TipoAtencion3.equals("no contiene") && TipoAtencion4.equals("no contiene")){
     TipoAtencionX = "TipoAtencion4";
} else if (!TipoAtencion1.equals("no contiene") && !TipoAtencion2.equals("no contiene") &&
     !TipoAtencion3.equals("no contiene") && !TipoAtencion4.equals("no contiene")
     && TipoAtencion5.equals("no contiene")){
      TipoAtencionX = "TipoAtencion5";
} else if (!TipoAtencion1.equals("no contiene") && !TipoAtencion2.equals("no contiene") &&
      !TipoAtencion3.equals("no contiene") && !TipoAtencion4.equals("no contiene")
      && !TipoAtencion5.equals("no contiene")){
      etnuevotap.setVisibility(View.INVISIBLE);
      Toast.makeText(getActivity(), "Ya tienes cinco tipos de atención registrados," +
      " no puedes tener más, elimina alguno.", Toast.LENGTH_SHORT).show();
}    

I hope you can help me, of course, many thanks for your time!

    
asked by Matías Nicolás Núñez Rivas 12.06.2018 в 16:08
source

2 answers

2

Your possible branches are

  • A

  • ! A & & amp; B

  • ! A & & amp; ! B & & C

  • ! A & & amp; ! B & & ! C & amp; & D

But, the else of A is precisely !A

if (A) {
    ...  //A
} else {
  // Aquí se cumple !A
  if (B) {
     ...  // !A && B
  } else {
    // Aquí se cumple !A && !B
    if (C) {
      ... // !A && !B && C
    } else {
      // Aquí se cumple !A && !B && !C && D
    }
  }

That said, when you get to this level it starts to be difficult to follow the values, and it starts to agree to rethink if it is possible to obtain the same in another way.

For example, since it is clear that you are looking for the first element "does not contain", simply put all the elements in a list or array and trace it until you find the first valid element.

    
answered by 12.06.2018 / 16:18
source
2

If you always meet the first condition you should not end it while you do not pass through the other conditions, you need to nest the ifs:

if (TipoAtencion1.equals("no contiene")){
  TipoAtencionX  = "TipoAtencion1";

} else {
     if (TipoAtencion2.equals("no contiene")) {
       TipoAtencionX  = "TipoAtencion2";
     } else {
           ........
     }
}

Or raise it from the most restrictive condition (in your case the last one) to the least restrictive so that it only enters the first if it meets all of them:

 if (!TipoAtencion1.equals("no contiene") && !TipoAtencion2.equals("no contiene") &&
    !TipoAtencion3.equals("no contiene") && !TipoAtencion4.equals("no contiene") &&
    !TipoAtencion5.equals("no contiene")) {
    etnuevotap.setVisibility(View.INVISIBLE);
    Toast.makeText(getActivity(), "Ya tienes cinco tipos de atención registrados," +
        " no puedes tener más, elimina alguno.", Toast.LENGTH_SHORT).show();
} else if (!TipoAtencion1.equals("no contiene") && !TipoAtencion2.equals("no contiene") &&
    !TipoAtencion3.equals("no contiene") && !TipoAtencion4.equals("no contiene") ) {
    TipoAtencionX = "TipoAtencion5";
} ....
    
answered by 12.06.2018 в 16:14