为什么Java不会从文件中读取这些行?

时间:2014-02-10 06:06:45

标签: java

编译并且一切正常但是当我尝试运行程序并输入输出文件时,它说它无法读取我的文件。我有一系列数字分别存储在一个名为“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  

2 个答案:

答案 0 :(得分:2)

您需要检查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();
}

答案 1 :(得分:0)

使用来源

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)中查看文件并关闭自动换行并打开不可见项,或者更好的是使用十六进制编辑器查看文件的内容。 / p>