I'm working on a program that is supposed to read numbers from a file and not only print them, but also find and report the average. However, I keep running into errors such as:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at FileRead.GetAvg(FileRead.java:36)
at FileRead.main(FileRead.java:18)
I'm a beginner and not sure what exactly is going wrong. If someone could let me know exactly what my errors are or steer me in the right direction if I'm completely wrong, it would be appreciated. I've been working at this for days and keep running into errors. Here is my code:
import java.util.*;
import java.io.*;
public class FileRead {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(System.in);
System.out.println("Input filename (Be sure to add .txt):");
String filename = input.nextLine();
File inputFile = new File(filename);
Scanner reader = new Scanner(inputFile);
ReadFile(reader);
GetAvg(reader);
}
public static void ReadFile(Scanner reader) {
System.out.println("The numbers are: ");
double count = reader.nextDouble();
System.out.println(count);
while (reader.hasNextDouble()){
for (int i=0; i<count; i++);{
System.out.println(reader.nextDouble());
}
}
}
public static void GetAvg(Scanner reader) {
double count = 8;
double numbers = reader.nextDouble();
double sum = 0;
for(int i=0; i <=count ; i++)
sum = sum + numbers;
double average = sum / count;
System.out.println("Average is : " + average);
}
}