javafile-iobufferedinputstream

NumberFormatException when reading from file sequentially?


Essentially the purpose of the program is to write student information to a file and read from the same file. The program includes an if statement to tell if the student is in good standing or academic probation, with respective files for each (goodstanding.txt and probation.txt). If I comment out the read logic for goodstanding.txt, the program works file, yet to me they seem nearly identical aside from the file path, obviously. I've looked through other questions but most of them seem to be related to faulty type-casting. The specific error it throws is NumberFormatException For input string: "" on line ID = Integer.parseInt(array[0]);

Here's the write logic:

if(GPA >= 2.0) //If GPA >= 2.0, write to good standing file. This is 
               identical to probation writer, but causes an issue somewhere
{
     String result = ID + "," + fname + " " + lname + "," + GPA;
     OutputStream out = new BufferedOutputStream(Files.newOutputStream(good, 
     StandardOpenOption.CREATE));
     BufferedWriter wt = new BufferedWriter(new OutputStreamWriter(out));
     wt.write(result, 0, result.length()); //Write to file from position 0 to 
                                        length
     wt.newLine();
     System.out.println("Please enter next WIN or 999 to quit: ");
     ID = input.nextInt();
     input.nextLine();
     wt.close();
}
     if(GPA < 2.0) //If GPA < 2.0, write to probation file {
     String result = ID + "," + fname + " " + lname + "," + GPA;
     OutputStream out = new 
                   BufferedOutputStream(Files.newOutputStream(probation, 
                   StandardOpenOption.CREATE));
     BufferedWriter wt = new BufferedWriter(new OutputStreamWriter(out));
     wt.write(result, 0, result.length());
     wt.newLine();
     System.out.println("Please enter next WIN or 999 to quit: ");
     ID = input.nextInt();
     input.nextLine();
     wt.close();
}

And the read logic:

try
{
    while(line != null)
{
    array = line.split(",");
    ID = Integer.parseInt(array[0]);
    name = array[1];
    GPA = Double.parseDouble(array[2]);
    double ahead = GPA - 2;
    System.out.println("WIN: " + ID + " | " + " Name: " + name + " |" + " GPA                 
                        = " + GPA + " | Ahead by " + ahead);
    line = reader.readLine(); 
}
}
catch(NullPointerException e)
    {System.out.println("NullPointerException occurred");}
reader.close();


InputStream input2 = new
                     BufferedInputStream(Files.newInputStream(probation));
BufferedReader reader2 = new BufferedReader(new InputStreamReader(input2));
String line2 = reader2.readLine();
System.out.println();
System.out.println("---------------------------");
System.out.println("--Academic Probation List--");
System.out.println("---------------------------");
try{
while(line2 != null)
{
    array = line2.split(",");
    ID2 = Integer.parseInt(array[0]);
    name2 = array[1];
    GPA2 = Double.parseDouble(array[2]);
    double fallShort = 2 - GPA2;
    System.out.println("WIN: " + ID2 + " | " + " Name: " + name2 + " |" + " 
                       GPA = " + GPA2 + " | Behind by " + fallShort);
    line2 = reader2.readLine();
}
}
catch(NullPointerException e)
{e.printStackTrace();}
reader.close();

I also tried using the trim() method on ID, but the exception remained. Is there a concept I need to read more about that would explain this?


Solution

  • The error message is clear i think NumberFormatException For input string: "" as you you can't parse an empty string to an int.

    There are at least two causes for your issue, either you have some blank lines in your file or one of your lines starts with a comma. Check that or ignore such lines by doing something like:

    ....
        while (line != null) {
            if(!line.startsWith(",") && !line.isEmpty()){
                array = line.split(",");
                ID = Integer.parseInt(array[0]);
                name = array[1];
                GPA = Double.parseDouble(array[2]);
                double ahead = GPA - 2;
                System.out.println("WIN: " + ID + " | " + " Name: " + name + " |" + " GPA = " + GPA + " | Ahead by " + ahead);
            }
            line = reader.readLine();
        }