1

so i just fixed an error then following that i got the exception error and not sure what to change to fix it. i've looked at similar issues but none of them seemed to pertain to my specific problem.

import java.util.Scanner;
import java.io.*;
import java.text.DecimalFormat;


public class AAAAAA {
    public static void main (String[] args)throws IOException {

    final String fileName = "classQuizzes.txt";
//1)
    Scanner sc = new Scanner(new File(fileName));

    //declarations 
    String input;
    double total = 0;
    double num = 0;
    double count = 0;
    double average = 0;
    String lastname;
    String firstname;
    double minimum;
    double max;


//2) process rows    
            while (sc.hasNextLine()) {
               input = sc.nextLine();
               System.out.println(input);



   //find total
               total += Double.parseDouble(input);  //compile error on using input
               count++; 

               System.out.println(count); //test delete later
   //find average (decimal 2 points)
               System.out.println("hi"); //test
               average = (double)total / count;
               System.out.println("Average = " + average);

//3) class statistics


            }

     }
}
Stuwuf
  • 9
  • 6

1 Answers1

1

It's actually a runtime exception not a compile error.

The reason is because your Scanner is reading through the whole file, line by line, and is hitting something that cannot be parsed as a double.

// for each line in the file
while (sc.hasNextLine()) {
    String line = sc.nextLine();
    System.out.println(line);

    // split the line into pieces of data separated by the spaces
    String[] data = line.split();

    String firstName = null;
    String lastName = null;

    // get the name from data[]
    // if the array length is greater than or equal to 1
    // then it's safe to try to get something from the 1st index (0)
    if(data.length >= 1)
        firstName = data[0];
    if(data.length >= 2)
        lastName = data[1];

    // what is the meaning of the numbers?

    // get numbers
    Double d1 = null;
    if(data.length >= 3){
        try {
            d1 = Double.valueOf(data[2]);
        } catch (NumberFormatException e){
            // couldn't parse the 3rd piece of data into a double
        }
    }

    Double d2 = null;
    // do the same...

    // do something with 'firstName', 'lastName', and your numbers ...
}