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 ...
}
xtratic
  • 4,600
  • 2
  • 14
  • 32
  • so doing that tells me all the rows in my the txt file could not be parsed. what should i do with this – Stuwuf Dec 06 '17 at 02:33
  • What does one of the invalid values of `input` look like. – xtratic Dec 06 '17 at 02:35
  • this is the first line " Joe Smith 20 31 25 33 43 could not be parsed as a double!" – Stuwuf Dec 06 '17 at 02:37
  • That's because "Joe Smith ..." is not a number. You will need to parse the additional data individually from that row. One sec, I'll edit my response to help you out some more. – xtratic Dec 06 '17 at 02:38
  • yes this was very helpful although there's an error saying .split has no suitable method found – Stuwuf Dec 06 '17 at 03:01
  • Please accept this as the answer then. Also what are you calling it on? `split()` is a method of `String`s so if you do `String line = sc.nextLine();` then `String[] data = line.split()` that should work. – xtratic Dec 06 '17 at 03:04
  • @Stuwuf no problem. keep studying! gl on exams if ya got 'em. and merry Christmas! – xtratic Dec 06 '17 at 03:16
  • you too! merry xmas – Stuwuf Dec 06 '17 at 03:31