Error in "switch case"

Asked

Viewed 221 times

1

I have a mistake in one of cases of switch, the mistake is this:

Erro mostrado

I’m doing a CRUD from a library, so when you see the code, maybe you think something’s weird:

I made some switch with only 1 option, as each option leads to a different menu

@Override
public int Menu(int menu) {
    int opc=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 registration menu, the second goes to the edit menu... so I thought I’d make a switch with an option for each menu.

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

The mistake happens right here:

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

My Github.

  • You can only sign the method Biblioteca.MenuEditar? It seems that you expect a boolean in the signature, but is passing an integer

  • I swear I tried to read your code, but it is not beautiful, which has just influenced the readability. I believe that, for each case of switch, it would be better to delegate to a function/method that deals with the desired. For example, case 1 calls the register function, which in turn would have a switch playing for methods of cadastrarAutor (case 1), cadastrarEditora (case 2) etc

  • public int menuEditar(int menu);

  • Tries to compile with javac and shows us the errors that the compiler releases, we may have more tips

3 answers

1

This error is happening because your switch ends before the case 2:, on line 188.

Like said Jefferson Quesado, your code is barely readable, both by formatting and over sentences in each case, and this makes it difficult to identify where each block ends and starts.

At his suggestion, modulate your code, putting case blocks into functions to simplify Switchs, 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 specialized in registering, another specialized in editing, another in listing, etc, that would know how to call the correct methods of the Library class.

  • 1

    I think this is more worthy of a pull request than in response

-1

The break is missing; There is a part of your case on github missing 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!");
     }
  • 1

    The absence of break does not generate syntax error. When you want to cascade the effect from one case to the next, just omit the break (but usually this is not what is desired)

-1

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

case 2:{        
     biblioteca.menuEditar(opc);
     break;     
}
  • 1

    The absence of break does not generate syntax error. When you want to cascade the effect from one case to the next, just omit the break (but usually this is not what is desired)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.