Problem with switch menu case and while

Asked

Viewed 1,113 times

1

My goal is that after I run one of the menu options and say I don’t want to continue on it, I can insert another option (or the same if I want to go back).

My code runs the first option I type but does not capture the second, it just continues to run the code. Ex: if I put that I want to launch, and then say that I no longer want to launch another student’s grades, he asks me the question of what I wish to do but regardless of my answer he executes the second case "grades". Follows the code:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;

public class Entrega {

    public static void main(String[] args) {
        String nomes [] = new String[100];
        double notas [][] = new double [100][5];
        String resposta = "S";
        double media = 0;
        double soma = 0;
        int qtdnomes = 0;
        String turma = "";
        String resposta1 = "s";
        String resposta2 = "s";
        Scanner input = new Scanner (System.in);
        System.out.println("O que deseja fazer? LANCAR notas, ver NOTAS, ver RESULTADOS, SAIR?" );       
        String opcao = input.nextLine();
        while (resposta.equalsIgnoreCase("s")){
            switch (opcao) {
            case "lancar":
                while(resposta1.equalsIgnoreCase("S")){
                    for(int i=0;i<=nomes.length;i++) {
                        input = new Scanner (System.in);
                        System.out.println("Qual o nome da turma?");
                        turma = input.nextLine();
                        input = new Scanner (System.in);
                        System.out.println("Qual o nome do aluno?");
                        nomes[i] = input.nextLine();
                        qtdnomes=qtdnomes+1;
                        for(int j=0;j<=3;j++) {
                            System.out.println("Qual a nota "+j);
                            notas[i][j]=input.nextDouble();
                            soma=soma+notas[i][j];
                            media=soma/4;
                            notas[i][4] = media;
                        }
                        System.out.println("Deseja lançar notas de outro aluno? S/N?");
                        resposta1 = input.next();
                        media=0;
                        soma=0;
                        input=null;
                        if(resposta1.equalsIgnoreCase("n")) {
                            resposta="n";
                            break;
                        }
                    }
                    try{
                        File medias = new File(turma);
                        FileWriter gravador = new FileWriter(medias,true);
                        for(int p=0;p<qtdnomes;p++){
                            gravador.write(nomes[p]+System.lineSeparator());
                            gravador.write(Arrays.toString(notas[p])+System.lineSeparator());
                            gravador.flush();
                        }
                    }   


                    catch(IOException e){
                        System.err.printf("Erro na gravação do arquivo: %s.\n",e.getMessage());
                    }

                    input = new Scanner (System.in);
                    System.out.println("O que deseja fazer? LANCAR notas, ver NOTAS, ver RESULTADOS, SAIR?" );       
                    opcao = input.nextLine();
                }
            case "notas":
                while(resposta2.equalsIgnoreCase("s")) {
                String nomearquivo="";
                input = new Scanner (System.in);
                System.out.println("Nome do arquivo de texto");
                nomearquivo = input.next();
                try {
                    FileReader arquivo = new FileReader(nomearquivo);
                    BufferedReader leitor = new BufferedReader(arquivo);
                    String linha;
                    while((linha = leitor.readLine())!=null){
                        System.out.println(linha);
                    }
                    input = new Scanner (System.in);
                    System.out.println("Deseja consultar outra turma? S/N?");
                    resposta2=input.nextLine();
                    if (resposta2.equalsIgnoreCase("n")) {
                        break;
                    }

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                }
            }
        }
    }

}
  • before receiving the value, calls nextLine() again, seems to be buffer.

  • Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

6

The switch has something called fallthrough automatic, ie it is not deleting, it will execute all options if the code leaves. To change this behavior need to put a break at the end of case, so it closes the switch excluding the execution of others.

This behavior is criticized by almost everyone, was a mistake of C and all languages copy saying it is to make the older programmers comfortable. But they change a lot of things in language that don’t make anything comfortable, so it’s a flawed argument.

Already the break used within the if will break the switch and I think you’re imagining that he will break the while. That’s another problem, the same command breaks different things.

It’s possible you have other problems, the code does a lot of things together, it’s hard to understand, and I’ve seen some things that would look better the other way. If you break this in methods you will be better, if you declare variables together with your use and not everything before you will be better, if you do not capture exception to do something useless you will be better. Making the code easier to read will make it better.

Browser other questions tagged

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