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();


}
}

Please repost using code tags and formatting/indentation:

[code=JAVA] // code goes here

[/code]

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();
	
	}
}

NumberFormatString?

it would help if you actually post your error (including line numbers), but my guess would be Double.parseDouble isn't working for you.

Here is my exact run-time error (the program compiles fine):

Exception in thread "main" java.lang.NumberFormatException: For input string; "Smith 13 20 8 12 -1"
at sun.misc.FloatingDecimal1.readJavaFormatString(Unknown Source)
at java.lan.Double.parseDouble(Unknown Source)
at Scoring.main(Scoring.java:45)


Just as an FYI: The "Smith 13 20 8 12 -1" is the first line of the file I am having Java read.

on a seperate, but still related note, you are importing the Scanner class, so you might as well use it =] You can use the scanner class to find the integers/doubles in each line. it would look something like this:

Scanner line = new Scanner(inputFile.readLine()); // Make a Scanner that looks at only the current line

int foo = line.nextInt(); // find the first group of integers in the String, calling nextInt() again returns the next group of ints

That's just a rough example, but something along those lines (you can replace nextInt with nextDouble, but I think it looks for decimal points to distinguish between ints and doubles.

If that was way out in left field and had nothing to do with what you were having problems with, I apologise.

Would I be able to also collect the name from each line using the same idea? I need to be able to display each line from the flie as it is presented and do mathematical calculations with the set of numbers within the lines.

if this is one line, how do you think to convert this to a number?
>> Smith 12 14 15 12 16 -1

I'm pretty sure Smith is not a decimal value. you'll need to split up this line in an array containing: one name, several numbers, the -1 (end-of-line)

on another note:
why are you doing this?
>> public static void main(String[] args) throws IOException

the last place possible to catch this, is in the main method.

If you tinker with it enough, it should be able to do that. You could remove the numbers from the String, which wouldn't be too hard to do. Then you could save what's left into a separate String. =]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.