How to add an else to a statement of more than one IF?

0

I have a problem in my project, I would like to know how to add an else statement to an "group" of If I have in my project, when I add the else of the group of if it tells me that the else has problems since it does not find the if. I hope you can help me, thank you very much in advance.

class Manejador implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        String pal;

        if(bcc.isEnabled()) {
            pal = JOptionPane.showInputDialog("Porfavor, digite el grupo que desea consultar:");
            {
                if(pal.matches("Grupo A"))
                    JOptionPane.showMessageDialog(null,pal+"\n Rusia\n Arabia Saudi\n Egipto\n Uruguay");

                if(pal.matches("Grupo B"))
                    JOptionPane.showMessageDialog(null,pal+"\n Portugal\n España\n Marruecos\n Irán");

                if(pal.matches("Grupo C"))
                    JOptionPane.showMessageDialog(null,pal+"\n Francia\n Australia\n Peru\n Dinamarca");

                if(pal.matches("Grupo D"))
                    JOptionPane.showMessageDialog(null,pal+"\n Argentina\n Islandia\n Croacia\n Nigeria");

                if(pal.matches("Grupo E"))
                    JOptionPane.showMessageDialog(null,pal+"\n Brasil\n Suiza\n Costa Rica\n Serbia");

                if(pal.matches("Grupo F"))
                    JOptionPane.showMessageDialog(null,pal+"\n Alemania\n Mexico\n Suecia\n Corea del Sur");

                if(pal.matches("Grupo G"))
                    JOptionPane.showMessageDialog(null,pal+"\n Bélgica\n Panamá\n Túnez\n Inglaterra");

                if(pal.matches("Grupo H"))
                    JOptionPane.showMessageDialog(null,pal+"\n Polonia\n Japon\n Colombia\n Senegal");

            }
        }
    }
}
    
asked by Diego Alejandro 11.03.2018 в 22:04
source

4 answers

4

It seems to me that what you have is a concept error. For each conditional if you can only have a else or else if associated. For example, if you have this code:

if (num1 == num2)
    System.out.println("Los números son iguales");
if (num1 > num2)
    System.out.println("El número 1 es mayor");
else
    System.out.println("El número 2 es mayor");

2 things are going to happen:
1 - You will always check both if , because they are independent of each other.
2 - The else corresponds only to the second if , so whenever it does not meet that num1 > num , the program will enter else and per% will be printed El número 2 es mayor . This will happen even if the numbers are the same, since the first if has nothing to do with the second.

To avoid the previous behavior, you should only use a else if , and this way it is guaranteed that when any of the conditions is met, the program does not continue checking the following ones. Example:

if (num1 == num2)
    System.out.println("Los números son iguales");
else if (num1 > num2)
    System.out.println("El número 1 es mayor");
else
    System.out.println("El número 2 es mayor");    

Now if the text El número 2 es mayor is printed only when both if are not met.

In your case as it would be:

        if(bcc.isEnabled()) {
            pal = JOptionPane.showInputDialog("Porfavor, digite el grupo que desea consultar:");

            if(pal.matches("Grupo A"))
                JOptionPane.showMessageDialog(null,pal+"\n Rusia\n Arabia Saudi\n Egipto\n Uruguay");
            else if(pal.matches("Grupo B"))
                JOptionPane.showMessageDialog(null,pal+"\n Portugal\n España\n Marruecos\n Irán");
            else if(pal.matches("Grupo C"))
                JOptionPane.showMessageDialog(null,pal+"\n Francia\n Australia\n Peru\n Dinamarca");
            else if(pal.matches("Grupo D"))
                JOptionPane.showMessageDialog(null,pal+"\n Argentina\n Islandia\n Croacia\n Nigeria");
            else if(pal.matches("Grupo E"))
                JOptionPane.showMessageDialog(null,pal+"\n Brasil\n Suiza\n Costa Rica\n Serbia");
            else if(pal.matches("Grupo F"))
                JOptionPane.showMessageDialog(null,pal+"\n Alemania\n Mexico\n Suecia\n Corea del Sur");
            else if(pal.matches("Grupo G"))
                JOptionPane.showMessageDialog(null,pal+"\n Bélgica\n Panamá\n Túnez\n Inglaterra");
            else if(pal.matches("Grupo H"))
                JOptionPane.showMessageDialog(null,pal+"\n Polonia\n Japon\n Colombia\n Senegal");
            else
                System.out.println();
        }

It is important to remember that the keys are only necessary if within the if or the else you need to have several lines of code, otherwise they are totally optional to use them or not.

    
answered by 11.03.2018 / 22:43
source
4

What you have there are individual if they are part of a block of code between { } , any else that you add will be part of the immediately higher if, if you add it at the end it will be part of the last if

if(pal.matches("Grupo H")){ ... } else { /* aca*/}

Since pal can not have several values at the same time, that is

pal.matches("Grupo A") and pal.matches("Grupo B") can not be true at the same time. What you need to do is re-write your conditionals

if(pal.matches("Grupo A")){ ... }
else if(pal.matches("Grupo B")) { ... }
...
else {
/** tus condiciones si no se cumplen ninguna de las anteriores**/
}

You could refactor your code to use a Map and reduce the amount of if s, for example:

Map<String,String> mapa = new HashMap<String,String>();
mapa.put("Grupo a", "Mensaje a...");
mapa.put("Grupo b", "Mensaje b...");
//rellenar el resto de grupos
if(mapa.containsKey(pal)){
//mostrar mensaje
}
else {
//ejecutar si no  se especificó ningún grupo
}
    
answered by 11.03.2018 в 22:45
0

Have you already tried putting keys at the beginning and end of each if? I know that without putting them it should work but I got to have that kind of problems and with some keys it was solved You can also deal with:

else if(condicion){
printf("Hola");
}else if(condicion){
printf("Adios");
}

Greetings.

    
answered by 11.03.2018 в 22:17
0

I understand that only one of the if is applied, in which case this should work:

class Manejador implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        String pal;

        if(bcc.isEnabled()) {
            pal = JOptionPane.showInputDialog("Porfavor, digite el grupo que desea consultar:");
            {
                if(pal.matches("Grupo A"))
                    JOptionPane.showMessageDialog(null,pal+"\n Rusia\n Arabia Saudi\n Egipto\n Uruguay");
                //A partir del segundo, else if en vez de solo if
                else if(pal.matches("Grupo B"))
                    JOptionPane.showMessageDialog(null,pal+"\n Portugal\n España\n Marruecos\n Irán");

                else if(pal.matches("Grupo C"))
                    JOptionPane.showMessageDialog(null,pal+"\n Francia\n Australia\n Peru\n Dinamarca");

                else if(pal.matches("Grupo D"))
                    JOptionPane.showMessageDialog(null,pal+"\n Argentina\n Islandia\n Croacia\n Nigeria");

                else if(pal.matches("Grupo E"))
                    JOptionPane.showMessageDialog(null,pal+"\n Brasil\n Suiza\n Costa Rica\n Serbia");

                else if(pal.matches("Grupo F"))
                    JOptionPane.showMessageDialog(null,pal+"\n Alemania\n Mexico\n Suecia\n Corea del Sur");

                else if(pal.matches("Grupo G"))
                    JOptionPane.showMessageDialog(null,pal+"\n Bélgica\n Panamá\n Túnez\n Inglaterra");

                else if(pal.matches("Grupo H"))
                    JOptionPane.showMessageDialog(null,pal+"\n Polonia\n Japon\n Colombia\n Senegal");

            }
        }
    }
}

In Java there is no such thing as applying a else in case no if of a group is executed. One way to achieve that effect is with switch-case using the default tag. If you can not use this, you can add to each of the conditional a variable that changes if you enter one of these, and finally a last conditional that checks if it has not been activated:

boolean activado = false;
if(condicion1){
    //...
    activado = true;
}
if(condicion2){
    //...
    activado = true;
}
if(condicion3){
    //...
    activado = true;
}
//Revisamos:
if(activado == false){//No entró en ninguno
    //...
}
    
answered by 11.03.2018 в 22:50