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();
}
}
http://download.oracle.com/javase/6/docs/api/java/util/Scanner.html
It appears you think you can pass a filename to
Scannerand have it open it.There is no constructor for
Scannerthat takes a file name as an argument in the way you think. What you have is a scanner that is reading over theString“scores.txt”The JavaDocs show you how to use the
Scannerclass properly to read in a file: