Getting into the wrong case

Asked

Viewed 324 times

2

I am doing an exercise in Java that asks to register, salary, tax and leave. I used a switch case for such, clear and so far so good.

However when I run and put to register Java travels the case 1 and before I even put the name of the individual it already jumps to the case 2.

I put break, I tried to do with a if not having to create another case, however the same thing happens when you have the if within the case 1.

I can’t understand why he doesn’t wait for the guy to give his name so after that it goes to the next code.

I’ll put the code down:

while(true) {
        System.out.println("Escolha \"1\" para cadastro; \"2\" para informar salário; \"3\" para calcular imposto; \"0\" para sair!");
        choice = scan.nextInt();

        choiceProgram: //LabeledLoops

        switch(choice) {

        case 1:
            while(true) {
                System.out.println("Informe o nome do indivíduo: ");
                listaFuncionarios.add(scan.nextLine());
                break choiceProgram;
            }
        case 2:
            System.out.println("Informe o nome: ");
            nome = scan.nextLine();
            nome = nome.toLowerCase();

            for(int i = 0; i < listaFuncionarios.size(); i++) {
                if(listaFuncionarios.equals(nome)) {
                    indice = i;
                    status = true;
                    break;
                }else if((i + 1) == listaFuncionarios.size() && status == false) {
                    System.out.println("Nome não encontrado!");
                    break choiceProgram;
                }
            }
            System.out.println("Informe o salário de " + listaFuncionarios.get(indice));
            listaSalario.set(indice, scan.nextDouble());
            break choiceProgram;

        case 3:
            System.out.println("Informe o nome do indivíduo que deseja calcular o imposto sobre salário: ");
            nome = scan.nextLine();
            nome = nome.toLowerCase();

            for(int i = 0; i < listaFuncionarios.size(); i++) {
                if(listaFuncionarios.equals(nome)) {
                    indice = i;
                    valor = listaSalario.get(indice);
                    status = true;
                    break;
                }else if((i + 1) == listaFuncionarios.size() && status == false) {
                    System.out.println("Nome não encontrado!");
                    break choiceProgram;
                }
            }

            System.out.println(listaFuncionarios.get(indice) + " recebe " + listaSalario.get(indice) + "\nCalculando o desconto do imposto, seu salário final é: " + Operacoes.Imposto(valor));

            break choiceProgram;

        case 0:
            System.out.println("O programa será finalizado!");
            break mainProgram;

        }

    }

Honestly I have no idea what’s going on and how I can make it work. Yes, no break choiceProgram (labeled loops) gives error with or without it. I will put an image of the console:

erro switch case

  • 1

    I believe I should have one break right outside the while in the first case.

1 answer

4


There are conflicts of break of case and of for which have slightly different functions. It has several ways to solve, but the most correct is to separate it into methods, so do not mix the two types of break:

while(true) {
    System.out.println("Escolha \"1\" para cadastro; \"2\" para informar salário; \"3\" para calcular imposto; \"0\" para sair!");
    choice = scan.nextInt();
    switch(choice) {
    case 1:
        Cadastro();
        break;
    case 2:
        Salario();
        break;
    case 3:
        Imposto();
        break;
    case 0:
        System.out.println("O programa será finalizado!");
        break mainProgram; //dá para fazer melhor que isto, mas não vi o resto
    }
}

Afterward:

private void Cadastro() {
    System.out.println("Informe o nome do indivíduo: ");
    listaFuncionarios.add(scan.nextLine());
}
private void Salario() {
    System.out.println("Informe o nome: ");
    nome = scan.nextLine().toLowerCase();
    for (int i = 0; i < listaFuncionarios.size(); i++) {
        if (listaFuncionarios.equals(nome)) {
            indice = i;
            status = true;
            break;
        } else if ((i + 1) == listaFuncionarios.size() && !status) {
            System.out.println("Nome não encontrado!");
            break;
        }
    }
    System.out.println("Informe o salário de " + listaFuncionarios.get(indice));
    listaSalario.set(indice, scan.nextDouble());
}
private void Imposto() {
    System.out.println("Informe o nome do indivíduo que deseja calcular o imposto sobre salário: ");
    nome = scan.nextLine()nome.toLowerCase();
    for (int i = 0; i < listaFuncionarios.size(); i++) {
        if (listaFuncionarios.equals(nome)) {
            indice = i;
            valor = listaSalario.get(indice);
            status = true;
            break;
        } else if ((i + 1) == listaFuncionarios.size() && !status) {
            System.out.println("Nome não encontrado!");
            break;
        }
    }
    System.out.println(listaFuncionarios.get(indice) + " recebe " + listaSalario.get(indice) + "\nCalculando o desconto do imposto, seu salário final é: " + Operacoes.Imposto(valor));
}

I put in the Github for future reference.

Avoid break named whenever possible. In case it is easier to do without it, then do without it. Only use it when it becomes easier to use it.

I see other problems in this code, but this is another matter. Even in this way you will probably have to change the declaration of variables that are not declared at the right place. But I can not help this part until because the code placed does not have everything that is necessary, and does not seem to be the focus of the question.

  • got it. I thought I’d add the information in Arraylist before using a method. But from what I see, the case almost always causes this kind of situation. I also tried to use Labeledloops to break the execution of the exact part I want. It seems to work. I will do what you are proposing pq see no alternative, but I would like to know a way this does not occur without having to appeal to a method.

  • Actually, this whole code is confusing, but like I said, I can’t help you anymore. I would start doing it again now in a more organized way, but you need to learn to organize code first. There flags, has an index that looks weird, and some other things.

  • i just posted the part related to the error. Above this code has the variables and the Arraylists being created.

  • Just take me a question @bigown, I necessarily have to create the Arraylists inside the main or can it be created in the method that is outside the main program? And if I create in the method has how to make another method access this Arraylist?

  • It depends on what you want to do, but that’s not even part of the doubt you posed. You can return the array, can make him stay in class, but we have no way of knowing without understanding the goal. I answered what was asked. Now you have another question.

  • grateful. I thought q could extend the case since q is part of the situation. I will create the Arraylist inside the main, until pq has several methods...

Show 1 more comment

Browser other questions tagged

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