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.
– Henrique Santiago
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).
– Maniero