| | |
Program help needed
![]() |
•
•
Join Date: Feb 2009
Posts: 9
Reputation:
Solved Threads: 0
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();
}
}
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();
}
}
•
•
Join Date: Feb 2009
Posts: 9
Reputation:
Solved Threads: 0
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.
it would help if you actually post your error (including line numbers), but my guess would be Double.parseDouble isn't working for you.
•
•
Join Date: Feb 2009
Posts: 9
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Aug 2008
Posts: 205
Reputation:
Solved Threads: 13
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:
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.
JAVA Syntax (Toggle Plain Text)
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.
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.
>> 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.
![]() |
Similar Threads
- How to Set permissions: e.g chmod 755 on FTP Program (Windows Software)
- Conceptual design help with C# program, feedback requested (C#)
- Help needed in graphics.h (C++)
- Help with efficient memory management (C)
- calculator program in C -help needed (C)
- help needed in assigning values.. (C)
- is there a program where there is only 32k in the main memory. (C++)
- All .exe files need another program to open them??!! (Windows 95 / 98 / Me)
- how can i make the program depend on time (C)
Other Threads in the Java Forum
- Previous Thread: deployment issues-java desktop database application
- Next Thread: Problem with reading in data from file
| Thread Tools | Search this Thread |
911 addball addressbook android api append applet application apps array arrays automation binary bluetooth businessintelligence button card chat class client code collision component crashcourse css csv database eclipse ee error fractal free game gis givemetehcodez graphics gui html ide image input integer integration j2me japplet java javaarraylist javadoc javafx javaprojects jni jpanel julia jvm linux list loan loop machine map method methods migrate mobile netbeans newbie objects oriented output panel phone physics problem program programming project radio recursion reporting scanner se server service set sms socket software sort sql string swing test textfield threads transfer tree trolltech ubuntu utility windows






