在Java中运行错误...扫描仪已关闭?

时间:2014-02-10 07:12:58

标签: java

所以我有这个代码,它应该从一个名为“Numbers.txt”的文件中读取数据。在这个文件中,它只是2003行十进制数。该程序的目标是读取此文件并进行计算。首先,我必须得到平均值,这是我设法做到的,但问题是我现在必须重新进入并获得标准偏差。它编译得很好,但我得到一个运行错误如下:

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

我不知道为什么我会收到这个错误。我从之前的平均计算中得到了一个类似的(但不是相同的),但对此的解决方案并不适用。有什么想法吗?

更改为inputFile2 ...

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Scanner.java:1115)
at java.util.Scanner.next(Scanner.java:1510)
at java.util.Scanner.nextDouble(Scanner.java:2456)
at StatsDemo.main(StatsDemo.java:51)

4 个答案:

答案 0 :(得分:4)

inputFile替换为inputFile2,因为它是Scanner对象。

答案 1 :(得分:2)

您的Scanner被称为inputFile2而不是inputFile

答案 2 :(得分:1)

在代码inputFile

中将inputFile2重命名为while (inputFile.hasNext())

答案 3 :(得分:0)

您的Scanner对象使用inputFile2,并且您使用inputFile作为扫描仪对象。我很惊讶您的代码是如何运行的,没有任何编译错误。如果您有另一个与inputFile Scanner对象相关联的文件,但您没有提供上面的代码,只需检查文件并关闭您正在刷新inputFile对象的那个引用。否则,请更正以下代码:

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 (inputFile2.hasNext()) //loop that continues until you are at the end of the file
    {
     difference = inputFile2.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 (inputFile2.hasNextDouble())
        inputFile2.nextLine(); //read a new line from the file
    }
  inputFile2.close(); //close the input file
    stdDev = Math.sqrt(sum/count); //store the calculated standard deviation