I need to display the original information from the input file (see below) as it is presented, compute and display each player's average score on his respective line.
Input file:
Smith 13 20 8 12 -1
Burch 21 18 16 -1
John -1
Mike 5 -1
(FYI) -1 is the sentinal.
Error I am getting at runtime:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at stats.main(stats.java:29)
My code line 29 is at "score = infile.nextDouble();" under the first while statement.
Any help will be greatly appreciated. THANKS!
import java.util.Scanner; //for reading input
import java.io.*; //for reading files
public class stats
{
public static void main(String[]args) throws IOException
{
String name=null;
double score=0, totalScore=0, count=0;
double avg=0;
File fin = new File("data.dat");
Scanner infile = new Scanner(fin);
while (infile.hasNext())
{
name = infile.nextLine();
score = infile.nextDouble();
while (score != -1)
{
System.out.print(name + " " + score + " ");
totalScore = totalScore + score;
score = infile.nextDouble();
System.out.print(score + " ");
count = count + 1;
avg = totalScore/count;
System.out.print("Players average: " + avg);
}
System.out.println(" ");
}
}
}