Reputation: 1179
I'm getting the error
Exception in thread "main" java.lang.NumberFormatException: For input string: "scores.txt"
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1222)
at java.lang.Double.parseDouble(Double.java:510)
at ExamAverage.main(ExamAverage.java:30)
C:\Users\Peter\AppData\Local\Temp\codecomp5461808896109186034.xml:312: Java returned: 1
for my code listed below what does this error mean? I'm trying to go line by line from a given text file path and output it in a certain format.
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.PrintWriter;
public class ExamAverage
{
public static void main(String[] args)
throws FileNotFoundException
{
String inputFileName = "scores.txt";
String outputFileName = "examScoreAverage.txt";
Scanner console = new Scanner(System.in);
Scanner in = new Scanner(inputFileName);
PrintWriter out = new PrintWriter(outputFileName);
int TestNumber = 1;
double totalPoints = 0;
double avg = 0;
double test = 0;
while (in.hasNextLine())
{
String line = in.nextLine();
test = Double.parseDouble(line);
out.println("Score" + TestNumber + " : " + test);
totalPoints += test;
TestNumber++;
}
avg = totalPoints/TestNumber;
out.println("Number of scores read: " + TestNumber );
out.println("Average Score " + avg );
in.close();
out.close();
} }
Upvotes: 1
Views: 1259
Reputation: 15703
You are trying to format a number from a string datatype. The java runtime are showing you the problem: java.lang.NumberFormatException: For input string: "scores.txt"
I didn't run the code, but debug it and cast the string number value to the correct datatype number format and it will work.
Upvotes: 0
Reputation: 428
Your in Scanner is not reading from the text file, it is actually reading the string literal "scores.txt" and then trying to parse "scores.txt" as a double. When constructing the Scanner to read from the file, make sure you pass a File, not the name of the file. It would look something like this:
Scanner in = new Scanner(new File("./scores.txt")):
Just make sure you pass the correct file path to File, not just the file name.
Upvotes: 0
Reputation: 51030
Exception in thread "main" java.lang.NumberFormatException: For input string: "scores.txt"
Looks like in the following:
test = Double.parseDouble(line);
line
contains "scores.txt"
instead of the content of the file.
Upvotes: 0
Reputation: 76898
http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html
It appears you think you can pass a filename to Scanner
and have it open it.
There is no constructor for Scanner
that takes a file name as an argument in the way you think. What you have is a scanner that is reading over the String
"scores.txt"
The JavaDocs show you how to use the Scanner
class properly to read in a file:
Scanner in = new Scanner(new File(inputFileName));
Upvotes: 2
Reputation: 61512
This line:
test = Double.parseDouble(line);
trys to create a double from the line variable. The error you are getting is because there is something other than a number in the line variable. Try printing out line before you call parseDouble()
Upvotes: 0