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

标签 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  

最佳答案

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

关于java - 为什么 Java 不从文件中读取这些行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21670152/

相关文章:

java - Akka 并发只有 1 个 actor

java数组线程安全

c# - 为什么在任何编程语言中都有声明默认 namespace /库的约定?

java - JTextField setEnabled 与 setEditable

java - 如何避免两个具有相同方法但没有公共(public)接口(interface)的类出现重复代码

java - 针对 C2 编译期间 Java 高内存使用率生成编译器重放数据文件

java - Android 应用程序在应用程序最小化并重新聚焦后从头开始启动。

java - Spring LDAP 1.3.0 澄清

java - 实现一个如果所有参数都在范围内则返回 true 的方法

java - Vim 语法高亮 java 中的标识符和函数