簡體   English   中英

為什么 Java 不會從文件中讀取這些行?

[英]Why wont Java read these lines from a file?

編譯一切正常,但是當我嘗試運行程序並輸入 output 的文件時,它說它無法讀取我的文件。 我有一系列數字分別存儲在一個名為“Numbers.txt”的文件中的單獨行中,它應該逐行獲取每個數字並最終計算平均值,然后將平均值寫入一個名為“Results.txt”的文件“

這里有錯誤:

 ----jGRASP exec: java StatsDemo

This program calculates statisticson a file containing a series of numbers
Enter the file name:  Results.txt
Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1585)
    at StatsDemo.main(StatsDemo.java:39)

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

我的代碼是:

File rf = new File("Numbers.txt"); //Create a FileReader object passing it the filename
    Scanner inputFile = new Scanner(rf); //Create a BufferedReader object passing it the FileReader object.
    //priming read to read the first line of the file
    while (inputFile.hasNext()) //create a loop that continues until you are at the end of the file
    {   
     sum += inputFile.nextDouble(); //convert the line into a double value and add the value to the sum
        count ++; //increment the counter
        inputFile.nextLine(); //read a new line from the file
  }
    inputFile.close(); //close the input file
    mean = sum/count; //store the calculated mean

如有必要,我可以發布更多程序。 我嘗試搜索,但找不到它為什么不讀取該行的答案。

更新:我繼續做標准差問題。 即使我使用了以前有效的新技術,我也會遇到類似的錯誤。

錯誤:

This program calculates statisticson a file containing a series of numbers
Enter the file name:  Results.txt
Exception in thread "main" java.lang.IllegalStateException: Scanner closed
    at java.util.Scanner.ensureOpen(Scanner.java:1115)
    at java.util.Scanner.hasNext(Scanner.java:1379)
    at StatsDemo.main(StatsDemo.java:49)

我的代碼:

        File rf2 = new File("Numbers.txt"); //reconnect to the FileReader object passing it the filename
    Scanner inputFile2 = new Scanner(rf2);//reconnect to the BufferedReader object passing it the FileReader object.
    sum = 0; //reinitialize the sum of the numbers
    count = 0; //reinitialize the number of numbers added
    //priming read to read the first line of the file
    while (inputFile.hasNext()) //loop that continues until you are at the end of the file
    {
     difference = inputFile.nextDouble() - mean; //convert the line into a double value and subtract the mean
        sum += Math.pow(difference,2); //add the square of the difference to the sum
        count++; //increment the counter
        if (inputFile.hasNextDouble())
        inputFile.nextLine(); //read a new line from the file
    }
  inputFile.close(); //close the input file
    stdDev = Math.sqrt(sum/count); //store the calculated standard deviation  

您需要檢查inputFile是否有下一行。 首次調用inputFile.nextDouble()后無法保證:

while (inputFile.hasNextDouble()){ //hasNextDouble() checks for Double availability
   sum += inputFile.nextDouble();
   count ++;
   if(inputFile.hasNextLine()){    //hasNextLine() checks for next Line availability
       inputFile.nextLine();
}

使用來源

Exception in thread "main" java.util.NoSuchElementException: No line found
    at java.util.Scanner.nextLine(Scanner.java:1585)

並且該來源通過搜索Scanner.java:1585輕松找到了互聯網

1578    public String nextLine() {
1579        if (hasNextPattern == linePattern())
1580            return getCachedResult();
1581        clearCaches();
1582
1583        String result = findWithinHorizon(linePattern, 0);
1584        if (result == null)
1585            throw new NoSuchElementException("No line found");
1586        MatchResult mr = this.match();
1587        String lineSep = mr.group(1);
1588        if (lineSep != null)
1589            result = result.substring(0, result.length() - lineSep.length());
1590        if (result == null)
1591            throw new NoSuchElementException();
1592        else
1593            return result;
1594    }

清楚地表明,有沒有在文件中匹配至於什么被認為是一條格局 ,任何 Scanner而言。

問題是以下之一:

  • 您可能是到達文件的結尾,因為要調用.nextXXX()方法兩次,單次檢查中.hasNext()

  • 文件沒有正確的行結束字符,所有內容都在一行上。

在適當的文本編輯器(NOT notepad.exe)中查看文件並關閉自動換行並打開不可見,或者更好的是使用十六進制編輯器查看文件的內容。

標准偏差的問題是您使用了錯誤的變量,您使用了 inputFile 而不是 inputFile2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM