Getting in the wrong case

2

I'm doing an exercise in Java that asks for registration, salary, tax and exit. I used switch case for this, of course and so far so good.

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

I put break , I tried to do with if so I do not have to create another case, however it does the same thing when I have if inside case 1 .

I can not understand why he does not wait for the guy to enter the name and then move on to the next code.

I'll put the code below:

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;

        }

    }

I honestly have no idea what's going on and how I can make it work. Yes, in break choiceProgram (labeled loops) gives error with or without this. I'm going to put an image of the console:

    
asked by anonymous 15.08.2017 / 17:57

1 answer

5

There are conflicts with break of case and for that have somewhat different functions. It has several ways to solve, but the most correct is to separate this 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
    }
}

Then:

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 placed GitHub for future reference .

Avoid% named_name whenever possible. In case it is easier to do without it, then do without it. Just use it when it's easier to use.

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

    
15.08.2017 / 18:08