The following code is giving me "Exception in thread main java.lang.NumberFormatString"
error and some specifics following it. The file I am having the program read is in the following REQUIRED format:
Smith 12 14 15 12 16 -1
James 19 19 28 48 12 -1
Where -1 is a sentinel value.
I'm assuming the problem is that the program can't read text and
numbers on the same line, which is required for my assignment. Any ideas? Thanks.
import java.util.Scanner; //Needed for the scanner class
import java.io.*; //Needed for the file classes
public class Scoring
{
public static void main(String[] args) throws IOException
{
String str1, str2, str3, str4;
double sum1=0, sum2=0, sum3=0, sum4=0, avg1=0, avg2=0, avg3=0, avg4=0, higherNumber, higherNumber1, higherNumber2;
//Open the file
FileReader freader = new FileReader("C:\\Users\\Erik\\Desktop\\javafiles\\Scores.dat");
BufferedReader inputFile = new BufferedReader(freader);
//Read the first line from the file
str1 = inputFile.readLine();
sum1 = Double.parseDouble(str1);
while (sum1 != -1)
{
avg1 = (sum1)/5;
}
//Read the second line from the file
str2 = inputFile.readLine();
sum2 = Double.parseDouble(str2);
while (sum2 != -1)
{
avg2 = (sum2)/5;
}
//Read the third line from the file
str3 = inputFile.readLine();
sum3 = Double.parseDouble(str3);
while (sum3 != -1)
{
avg3 = (sum3)/5;
}
//Read the fourth line from the file
str4 = inputFile.readLine();
sum4 = Double.parseDouble(str4);
while (sum4 != -1)
{
avg4 = (sum4)/5;
}
//Display the original information
System.out.println(str1 + " average score: " + avg1);
System.out.println(str2 + " average score: " + avg2);
System.out.println(str3 + " average score: " + avg3);
System.out.println(str4 + " average score: " + avg4);
//Calculate the highest average score
higherNumber1 = Math.max(avg1, avg2);
higherNumber2 = Math.max(higherNumber1, avg3);
higherNumber = Math.max(higherNumber2, avg4);
//Display the highest average scoring person
if (higherNumber == avg1)
System.out.println("Smith is the highest average scoring player");
else if (higherNumber == avg2)
System.out.println("Burch is the highest average scoring player");
else if (higherNumber == avg3)
System.out.println("Winoburg is the highest average scoring player");
else if (higherNumber == avg4)
System.out.println("James is the highest average scoring player");
inputFile.close();
}
}