Problem with menu switch case and while

1

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

My code executes the first option that I type but does not pick up the second, just continue to run the code. Ex: if I put that I want to launch, and then say that I do not want to release notes of another student, he asks me the question of what I want to do however regardless of my answer he executes the second case "notes". Here is 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();
                }
                }
            }
        }
    }

}
    
asked by anonymous 30.06.2017 / 06:26

1 answer

6

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

This behavior is criticized by almost everyone, it was a C error and all languages copy saying that it is to make older programmers comfortable. But they change a lot of thing in the language that do not make anything comfortable.

Since the break used within if will break switch and I think you're imagining that they will break while .

It is possible that you have other problems, the code does a lot of things together, it is difficult to understand, and I have seen some things that would be better in another way. If breaking this into methods will be better, if you declare the variables next to your use and not everything will get better, if not catching the exception to do something useless will be better. Making the code easier to read will be better.

    
30.06.2017 / 12:31