不确定将字符串转换为double

时间:2014-10-24 16:01:04

标签: java

对于这个程序,我打算获取一个文件(numbers.txt)并查找数字的平均值和标准差,并将它们打印在另一个.txt文件中。我相信我在这里遇到的问题包括字符串和转换它们,所以我可以在我的等式中使用它们。以下是我的代码片段:

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            
   }
}

编辑: 以下是我收到的错误消息:

java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Unknown Source)
at java.util.Scanner.hasNext(Unknown Source)
at java.util.Scanner.hasNextDouble(Unknown Source)
at StatsDemo.main(StatsDemo.java:48)
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)

1 个答案:

答案 0 :(得分:0)

所发生的是你执行while循环的一次迭代然后用inputFile2.close();关闭它,并且在关闭之后,while循环检查它是否需要用inputFile2.hasNextDouble()进行另一次迭代,但由于你刚关闭了扫描仪,它无法检查下一个双倍是否可用!

您可以通过在关闭扫描仪后放置break以退出while循环或将行IllegalStateException移出while循环来解决inputFile2.close();错误。

As I said before,您应该学会自己阅读和理解您的错误消息。您可以在错误消息中看到以下内容:at StatsDemo.main(StatsDemo.java:48)。它告诉您查看程序的第48行。我怀疑while (inputFile2.hasNextDouble())在你的代码中是第48行(你没有在你的问题中告诉我们,如果你这样做会很好......)。然后你应该想知道:“为什么我的扫描仪此时关闭?”。然后你应该看看你关闭扫描仪的位置。你会看到你只关闭第59行的扫描仪:inputFile2.close();。所以你会知道该行被执行了。然后你应该想:“这是我想要的行为吗?我想在那里关闭我的扫描仪吗?”。如果是,则放置 break ,这样您就不会在while循环中再次检查 hasNextDouble()。如果不是,则将 close()移动到您真正想要的其他位置。

如果您想知道代码的哪些行以哪种顺序执行,您还可以use a debugger(大多数IDE都有一行)。或者通过放置println("test 1")println("test 2")等进行调试(它有时对快速调试很有帮助,但最好使用调试器)

而且,正如其他人已经说过的那样,你应该真的在formatting your questions上工作。清楚地陈述您的问题,使问题尽可能具体,展示您理解的内容和您不理解的内容,并展示实际的研究工作。互联网上充满了Java教程和代码示例,可以解释您想要执行的这些简单事情,因此您可以更好地进行研究。

此外,您的问题的标题确实具有误导性。您在问题中发布的错误与将字符串转换为双精度无关。

祝学习Java好运!

相关问题