我得到下面這個錯誤是什麼意思列出我的代碼錯誤TXT文件輸入錯誤的Java
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
?我試圖從給定的文本文件路徑中逐行進行輸出,並以某種格式輸出。
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();
} }
顯示'scores.txt'的內容。也許並非文件中的所有行都包含數字,所以'parseDouble'無法將行的「內容」轉換爲數字。 – Zabba
'scores.txt'的內容是不相關的,因爲該文件的內容不會被讀取或解析;) –