繁体   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