Program help needed

Reply

Join Date: Feb 2009
Posts: 9
Reputation: EJD is an unknown quantity at this point 
Solved Threads: 0
EJD EJD is offline Offline
Newbie Poster

Program help needed

 
0
  #1
Mar 1st, 2009
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();

}
}
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,818
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Program help needed

 
0
  #2
Mar 1st, 2009
Please repost using code tags and formatting/indentation:

[code=JAVA]
// code goes here
[/code]
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 9
Reputation: EJD is an unknown quantity at this point 
Solved Threads: 0
EJD EJD is offline Offline
Newbie Poster

Re: Program help needed

 
0
  #3
Mar 1st, 2009
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();
	
	}
}
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Program help needed

 
0
  #4
Mar 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 9
Reputation: EJD is an unknown quantity at this point 
Solved Threads: 0
EJD EJD is offline Offline
Newbie Poster

Re: Program help needed

 
0
  #5
Mar 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 205
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training

Re: Program help needed

 
0
  #6
Mar 1st, 2009
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:
  1. Scanner line = new Scanner(inputFile.readLine()); // Make a Scanner that looks at only the current line
  2.  
  3. 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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 9
Reputation: EJD is an unknown quantity at this point 
Solved Threads: 0
EJD EJD is offline Offline
Newbie Poster

Re: Program help needed

 
0
  #7
Mar 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: Program help needed

 
0
  #8
Mar 2nd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 205
Reputation: llemes4011 is an unknown quantity at this point 
Solved Threads: 13
llemes4011 llemes4011 is offline Offline
Posting Whiz in Training

Re: Program help needed

 
0
  #9
Mar 3rd, 2009
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. =]
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC