0
In my last post (do/while repeats the expression twice) was having an input overlay problem using the Scanner class, and according to the links placed in the answer, we cannot use nextLine() after nextInt().
Now I’m making another algorithm and falling into the same problem. According to the link solution used as a response, I used Integer.parseint() to solve the problem. It turns out that java.lang.Numberformatexception: For input string: ""
From what I understand searching in English is because the String is empty. Ok, it is. But it is because it returns the error even before I can type, and has a row before asking for the input of a value in the String.
As you can see in the image above, after I insert the third note it already gives the error, before I put the frequency.
Follows the code:
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("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.");
}
}
Behold this your question and all those marked as duplicates attentively. The problem is the same. the
parseInt
that you put it doesn’t change anything because it continues to havenextDouble
followed bynextLine
– Isac