Java中运行错误…扫描程序已关闭?
我有这个代码,它应该从一个名为“Numbers.txt”的文件中读取数据。在这个文件中,它仅仅是2003行十进制数。该程序的目标是读取该文件并进行计算。首先我必须得到平均值,我已经设法做到了,但问题是我现在必须回去得到标准偏差。它编译得很好,但我得到一个运行错误,如下所示:Java中运行错误…扫描程序已关闭?,java,Java,我有这个代码,它应该从一个名为“Numbers.txt”的文件中读取数据。在这个文件中,它仅仅是2003行十进制数。该程序的目标是读取该文件并进行计算。首先我必须得到平均值,我已经设法做到了,但问题是我现在必须回去得到标准偏差。它编译得很好,但我得到一个运行错误,如下所示: Exception in thread "main" java.lang.IllegalStateException: Scanner closed at java.util.Scanner.ensureOpen(S
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 (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
这是我的代码:
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)
您的
扫描仪
称为inputFile2
而不是inputFile
将inputFile
替换为inputFile2
,因为它是扫描仪
对象。将inputFile
重命名为inputFile2
,而(inputFile.hasNext())您的扫描仪对象带有inputFile2,并且您正在使用inputFile作为扫描仪对象。我很惊讶你的代码是如何在没有编译错误的情况下运行的。如果您有另一个与inputFile Scanner对象关联的文件,而您没有提供上面的代码,只需检查该文件并关闭刷新inputFile对象的引用即可。否则,请按以下方式更正代码:
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 (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
---别介意我以前的帖子。我是个白痴。断然的。该睡觉了。谢谢大家。没关系,亲爱的。我很乐意帮助你们