Error converting String to Numbers in Java

0

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.");

}

}

    
asked by anonymous 18.01.2018 / 00:19

2 answers

0

use next (); instead of nextLine () when picking up frequency

frequencia = read.next();
    
18.01.2018 / 00:55
1

This happens because nextDouble() (or nextInt() , nextFloat() and etc) do not work the same as nextLine() .

nextLine() retrieves the next line of its input, since nextDouble() retrieves the next double of its input, which does not necessarily have to be the entire contents of the line.

For example: if you create the instance of Scanner you do:

Scanner read = new Scanner(System.in).useDelimiter(" ");

And in execution, at the moment of informing the first note, instead of passing a single value, pass three values separated by space (ex: 1 2 3 (has a last space after 3 )), capture of notes 2 and 3 will occur automagically because these methods do not read the entire line but only the next Double .

To solve the problem there are basically two options:

  • Call method nextLine() after each call of nextDouble()

  • Or change from nextDouble() to nextLine() and make conversions "on hand" (something like Double.parseDouble(val) )

18.01.2018 / 01:09