I have program that is suppose to ask the user what txt file, go through the txt file and find all parsable ints and average them. I have the following code below, but it's giving me a bunch of errors. What is the cause of all these errors?
The txt file is: 5 15 312 16 eight seven 44 eighty-five thousand and sixty-two 13 98 93
import java.util.Scanner;
public class Ch12Pt2 {
public static void main(String[] args) throws NumberFormatException {
Scanner input = new Scanner(System.in);
System.out.print("Enter filename: ");
String filename = input.nextLine();
Scanner file = new Scanner(filename);
if(file.nextLine().equals(""))
{
System.err.println("Could not find file:" + filename);
System.exit(1);
}
do {
try {
int total = 0;
int count = 0;
int num = file.nextInt();
total = num + total;
//Display the results
System.out.println("The number of parsable numbers: " + count);
System.out.println("Average values: " + (total / count));
}
catch (NumberFormatException ex) {
System.out.println("Cannot parse " + num + " as an integer.");
file.nextInt();
}
} while (file.hasNextInt());
// Close the files
input.close();
file.close();
}
}
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Ch12Pt2.main(Ch12Pt2.java:21)