In my last post ( do / while double-repeat the expression ) had an overlapping input problem using the Scanner class, and according to the links placed in the response, we can not use nextLine () after nextInt ().
Now I'm doing another algorithm and it falls into the same problem. According to the link solution used in response, I used Integer.parseInt () to solve the problem. It occurs that the java.lang.NumberFormatException error is now occurring: For input string: ""
From what I understand, searching in English is because the String is empty. Okay, that's it. But it is because it returns the error before I can even type it, and it has a line before asking for a value to enter in the String.
Asyoucanseeintheimageabove,afterIinsertthethirdnoteitalreadygivestheerror,beforeIputthefrequency.
Followthecode:
publicstaticvoidmain(String[]args){Scannerread=newScanner(System.in);DecimalFormatdf=newDecimalFormat("0.00");
String matricula;
String resultado;
String frequencia = "0";
int freq;
double nota1, nota2, nota3, notaFinal;
double maiorNota;
double menorNota;
double mediaGeral = 0;
int totalRep = 0;
double percentRepFreq;
for (int i = 1; i <= 5; i++) {
System.out.print("Entre com a matrícula do aluno " + i + ": ");
matricula = read.nextLine();
System.out.print("Digite as 3 notas do aluno " + i + "\nNota 1: ");
nota1 = read.nextDouble();
System.out.print("Nota 2: ");
nota2 = read.nextDouble();
System.out.print("Nota 3: ");
nota3 = read.nextDouble();
System.out.print("Digite a frequência do aluno " + i + ": ");
frequencia = read.nextLine();
freq = Integer.parseInt(frequencia);
if (freq < 40) {
totalRep += 1;
}
notaFinal = (nota1 + nota2 + nota3) / 3;
mediaGeral += notaFinal;
if (notaFinal >= 6 && freq >= 40) {
resultado = "Aprovado.";
} else {
resultado = "Reprovado.";
}
System.out.println("Aluno " + i + ", com matrícula " + matricula + ", teve frequência de " + frequencia
+ ", nota final: " + notaFinal + " e foi " + resultado);
System.out.println(" ");
}
percentRepFreq = (int) totalRep / 5 * 100;
System.out.println("Media geral da turma: " + mediaGeral);
System.out.println("Total de alunos reprovados: " + totalRep);
System.out.println(percentRepFreq + "% " + "dos alunos foram reprovados por frequência.");
}
}