gpt4 book ai didi

java - 打印/文件编写器出现问题

转载 作者:行者123 更新时间:2023-12-01 12:22:50 25 4
gpt4 key购买 nike

对于这项作业,我要编写一个代码,该代码将获取一个文件 (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 与平均值减法结合使用时遇到了一些困难。我相信我必须将该行转换为 double 值。这是我遇到一点麻烦的部分:

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
}

最佳答案

您关闭了 Scanner::inputFile 然后再次使用它。正如你想象的那样,如果你关闭了它,你就无法使用它了!

关于java - 打印/文件编写器出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26548778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com