Error in "switch case"

0

I have an error in one of the case s of switch , the error is this:

I'mdoingaCRUDfromalibrary,whenyouseethecode,youmightfinditstrange:

I'vemadesomeswitchwithonly1option,becauseeachoptionleadstoadifferentmenu

@OverridepublicintMenu(intmenu){intopc=0;System.out.println("-======MENU======-\n\n" +
            "1. Cadastrar\n" +
            "2. Editar\n" +
            "3. Pesquisar\n" +
            "4. Listar\n" +
            "5. Excluir\n" +
            "6. Excluir tudo\n" +
            "7. Sair");
    return opc;
}

The first option goes to the register menu, the second one goes to the edit menu ... so I thought it best to make a switch with an option for each menu.

I will make the code available to you on my GitHub, as it is too long to put here, the error is in the Main class, more specifically in line 198, where you have written:

The error happens exactly here:

case  2 : {         
    Biblioteca.MenuEditar(opc);
}

My Github .

    
asked by anonymous 28.06.2017 / 21:13

3 answers

1

This error is occurring because your switch ends before case 2: , at line 188.

As Jefferson Quesado , your code is barely legible, both by formatting and by the excess of sentences in each case, and this makes it difficult to identify where each block ends and where.

At his suggestion, modularize your code by putting case blocks into functions to simplify switches, for example:

    // primeiro switch: opc = biblioteca.menu()
    switch(opc) {
        case 1: cadastrar(); break;
        case 2: editar(); break;
        case 3: pesquisar(); break;
        case 4: listar(); break;
        case 5: excluir(); break;
        case 6: excluirTudo();
        case 7: sair(); break;
        default: break;
    }

    // segundo switch: opc = biblioteca.menuCadastrar()
    switch(opc) {
        case 1: cadastrarAutor(); break;
        case 2: cadastrarEditora(); break;
        case 3: cadastrarLivro(); break;
        case 4: voltar(); break;
        case 5: sair(); break;
        default: break;
    }

You could also, for example, have classes for each case of the first menu: a class that specializes in registering, another specialized in editing, another in list, etc., that would know how to call the correct methods of the Library class.     

29.06.2017 / 01:59
-1

The break is missing; There is a part of your case in github that lacks the break. here's an example:

int diaDaSemana = 1;
    switch (diaDaSemana) {
        case 1:
            System.out.println("Domingo");
            break;
        case 2:
            System.out.println("Segunda-feira");
            break;
        case 3:
            System.out.println("Terça-feira");
            break;
        case 4:
            System.out.println("Quarta-feira");
            break;
        case 5:
            System.out.println("Quinta-feira");
            break;
        case 6:
            System.out.println("Sexta-feira");
            break;
         case 7:
            System.out.println("Sábado");
            break;
        default:
             System.out.println("Este não é um dia válido!");
     }
    
28.06.2017 / 21:16
-1

The error occurs because the break is missing in this part of the code:

case 2:{        
     biblioteca.menuEditar(opc);
     break;     
}
    
28.06.2017 / 22:53