4

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.

Pedro Lourenço
  • 605
  • 7
  • 16
  • 2
    You forgot to add a break statement after each case... – Alexis C. Mar 27 '14 at 22:56
  • In fact, you probably said to yourself "Oh, gimme a break!" several time while fighting with it. Next time listen to what you say. – Hot Licks Mar 27 '14 at 23:03
  • No, I started with studying Java on my coledge about 2 weeks ago. And I didn't knew the break was mandatory to use in each case. Now it makes sense. – Pedro Lourenço Mar 27 '14 at 23:24
  • It's not "manditory" -- the compiler (as you know) will not object if you omit it, and there are legitimate cases where it might be omitted. But as a general rule, each `case` needs a corresponding `break` (except the very last `case` or `default` in a `switch`). – Hot Licks Mar 28 '14 at 01:43

2 Answers2

12

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

jmj
  • 237,923
  • 42
  • 401
  • 438
1

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  
}
fastcodejava
  • 39,895
  • 28
  • 133
  • 186