Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java brackets bug in SWITCH statement? [duplicate]

This is driving me crazy! I've searched through my entire code and I can't seem to solve this problem. Here is my code:

class Subtarefas {
public static void resolve (int flag, int na, Aeroporto a[], int nv, Voo v[]) {
    switch(flag) {
        //------------------------------------------------------------------
        case 3: {//Incompleto
            String mat[][] = new String [na][2];
            int count=0;
            int bigcount=0;
            int indice=0;
            int np=0;

            for (int i=0; i<na; i++) {
                if ( indexOf(np, mat, a[i].nomecidade, a[i].nomepais)== -1 ) {
                    mat[np][0]=a[i].nomecidade;
                    mat[np][1]=a[i].nomepais;
                    np++;
                }
            }

            for (int i=0; i<np; i++) {
            count=0;
                for (int j=0; j<np; j++) {
                    if (mat[i][1].equals(mat[j][1])) count++;
                }
                if (count>bigcount) {bigcount=count; indice=i;}
            }

            System.out.println(mat[indice][1] + " " + bigcount);
        }
        //------------------------------------------------------------------
        case 4: {//Feito
            String mat1[][] = new String [nv][2];
            int count=0;
            int bigcount=0;
            int indice=0;

            for (int i=0; i<nv; i++) {
                for (int j=0; j<na; j++) {
                    if (v[i].origem==a[j].cod) mat1[i][0]=a[j].nomepais;
                    if (v[i].destino==a[j].cod) mat1[i][1]=a[j].nomepais;
                }
            }

            for (int i=0; i<na; i++) {
            count=0;
                for (int j=0; j<nv; j++) {
                    if (a[i].nomepais.equals(mat1[j][0])) {
                        if (mat1[j][0].equals(mat1[j][1])) count++;
                    }
                }
                if (count>bigcount) {bigcount=count; indice=i;}
                else if (count==bigcount) {
                    int result = a[i].nomepais.compareTo(a[indice].nomepais);
                    if (result<0) {bigcount=count; indice=i;}
                }
            }

            System.out.println(a[indice].nomepais + " " + bigcount);
        }
        //------------------------------------------------------------------
        default: break;
    }
}

So basically I can't find what is wrong here, I can't filter the type {} brackets. There is some kind of bug here, because when I use case 3 I get two outputs, and it is suppose to give me only 1 (I only have 1 System.out.println in that case).

I found out when I use case 3 it also goes through case 4. That's not suppose to happen! What should I do?

Another option is that there could be a opening bracket { without a closing one }. But I can't find it!

Can you help me?

Thanks.

like image 385
Pedro Lourenço Avatar asked Dec 11 '22 06:12

Pedro Lourenço


2 Answers

because need to break; after each case to avoid execution of rest of the cases, read through switch case

like image 176
jmj Avatar answered Dec 29 '22 04:12

jmj


You can try :

switch(flag) {
    case 3: //Feito
    // your code;
    break;  // This makes it stop the switch
    case 4: //Feito 
    // your code;
    break;  // put break here if more case follows  
}
like image 22
fastcodejava Avatar answered Dec 29 '22 04:12

fastcodejava