对于这个分配,我要编写一个代码来获取文件(Numbers.txt)并计算数字的平均值和标准差,并将它们打印到不同的文件(Results.txt)。我的代码编译得很好但是当我进入我的键盘输入Numbers.txt时,我收到一条错误信息。
以下是我的代码片段
filename = keyboard.nextLine();
File file = new File(filename); //Create a FileReader object passing it the filename
Scanner inputFile = new Scanner(file);
line = inputFile.nextLine(); //priming read to read the first line of the file
while (inputFile.hasNextDouble()) //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.hasNext(); //read a new line from the file
}
inputFile.close(); //close the input file
mean = (sum/count); //store the calculated mean
File file2 = new File(filename); //Create a FileReader object passing it the filename
Scanner inputFile2 = new Scanner(file2); //reconnect to the FileReader object passing it the filename
//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
line = inputFile2.nextLine(); //priming read to read the first line of the file
while (inputFile2.hasNextDouble()) //loop that continues until you are at the end of the file
{
sum += inputFile2.nextDouble();
inputFile2.nextDouble();
}
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.hasNext(); //read a new line from the file
inputFile2.close(); //close the input file
stdDev = Math.sqrt(sum/count); //store the calculated standard deviation
这是我得到的错误消息
java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextDouble(Unknown Source)
at StatsDemo.main(StatsDemo.java:53)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
我确定我的编码出现了很多错误,但我还没有足够的经验来注意它们。非常感谢帮助。
最后,让程序运行并计算平均值,但现在我有点困难找出如何使用nextDouble和减法均值。我相信我必须将线转换为双倍值。以下是我遇到麻烦的部分:
File file2 = new File(filename); //Create a FileReader object passing it the filename
Scanner inputFile2 = new Scanner(file2); //reconnect to the FileReader object passing it the filename
//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
line = inputFile2.nextLine(); //priming read to read the first line of the file
while (inputFile2.hasNextDouble()) //loop that continues until you are at the end of the file
{
sum += inputFile2.nextDouble();
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.hasNext(); //read a new line from the file
inputFile2.close(); //close the input file
stdDev = Math.sqrt(sum/count); //store the calculated standard deviation
}
答案 0 :(得分:1)
您关闭了Scanner :: inputFile,然后再次使用它。你可以想象,如果你关闭它就不能使用它!
答案 1 :(得分:0)
将第47行从line = inputFile.nextLine();
更改为line = inputFile2.nextLine();
(as suggested by user1598503)后,您现在可以在第53行获得NoSuchElementException
。
它基本上说你的扫描仪没有下一个双倍。这是非常合乎逻辑的,因为你在第48行有一个while循环,它循环直到它到达文件的末尾。因此,当您在while循环之后请求下一个double时,您位于文件的末尾,并且没有下一个double。 The Java docs explain quite clearly why NoSuchElementException
被抛出。
您需要更改while循环。尝试更多地使用它和/或找到一个好的Java教程。我确定你是否花更多的时间来使你的计划有效。
祝你好运!