2016-11-20 72 views
-4

这是我运行StatsDemo时得到的错误。该程序不应该输出到控制台,但运行该程序将会在输出中创建一个名为Results.txt的文件。你应该得到的结果是:你应该得到77.444的平均值和10.021的标准偏差。即使我没有编译错误,我为什么在运行程序时遇到此错误?

运行StatsDemo。

这个程序计算含有一系列数字

的文件统计输入文件名:[DrJava输入框]

java.util.NoSuchElementException:没有找到行
在java中。 util.Scanner.nextLine(Scanner.java:1540)
在StatsDemo.main(StatsDemo.java:34) 在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)
在sun.reflect.NativeMethodAccessorImpl.invok E(NativeMethodAccessorImpl.java:62)
在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
在java.lang.reflect.Method.invoke(Method.java:498)
在edu.rice .cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)

下面是代码:

import java.text.DecimalFormat; //for number formatting 
import java.util.Scanner; //for keyboard input 
import java.io.*; //for using files 

public class StatsDemo 
{ 
    public static void main(String [] args)throws IOException { 

     double sum = 0; //the sum of the numbers 
     int count = 0; //the number of numbers added 
     double mean = 0; //the average of the numbers 
     double stdDev = 0; //the standard deviation of the numbers 
     String line; //a line from the file 
     double difference; //difference between the value and the mean 

     //create an object of type Decimal Format 
     DecimalFormat threeDecimals = new DecimalFormat("0.000"); 
     //create an object of type Scanner 
     Scanner keyboard = new Scanner (System.in); 
     String filename; // the user input file name 

     //Prompt the user and read in the file name 
     System.out.println("This program calculates statistics" 
      + " on a file containing a series of numbers"); 
     System.out.print("Enter the file name: "); 
     filename = keyboard.nextLine(); 

     //ADD LINES FOR TASK #4 HERE 
     File rf = new File("Numbers.txt"); 
     Scanner inputFile = new Scanner(rf); 

     while (inputFile.hasNextDouble()) 
     { 
      sum += inputFile.nextDouble(); 
      count ++; 
      inputFile.nextLine(); 
     } 
     inputFile.close(); 
     mean = sum/count; 

     //ADD LINES FOR TASK #5 HERE 
     File rf2 = new File("Numbers.txt"); 
     Scanner inputFile2 = new Scanner(rf2); 
     sum = 0; 
     count = 0; 
     //priming read to read the first line of the file 

     while (inputFile.hasNext()) 
     { 
      difference = inputFile.nextDouble() - mean; 
      sum += Math.pow(difference,2); 
      count++; 
      if (inputFile.hasNextDouble()) 
       inputFile.nextLine(); 
     } 
     inputFile.close(); 
     stdDev = Math.sqrt(sum/count); 

     //ADD LINES FOR TASK #3 HERE 
     FileWriter fwriter = new FileWriter("Numbers.txt", true); 
     PrintWriter outputFile = new PrintWriter(fwriter); 
     outputFile.print("mean = " + mean); 
     outputFile.print("deviation = " + stdDev); 
     outputFile.close(); 
     System.out.println("Data written to file"); 
    } 
} 
+4

缺少编译器错误并不意味着程序没有bug,实际上你的程序不能处理你给它的输入。虽然我们无法帮助你,但没有代码。 –

+0

因为你做错了什么。 –

+0

因为这是运行时错误。你关了扫描仪,也许?没有线路被发现...很难说没有你的代码 –

回答

0

你的错误似乎是从这一行来:

inputFile.nextLine(); 

我猜你的输入文件Numbers.txt没有超出当前阅读点的换行符。

这里的循环:

 while (inputFile.hasNext()) 
     { 
     difference = inputFile.nextDouble() - mean; 
      sum += Math.pow(difference,2); 
      count++; 
      if (inputFile.hasNextDouble()) 
      inputFile.nextLine(); 
     } 

.hasNext()默认查找任何空白,不只是线条,让你可以进入这个循环没有可用换行。如果你不明白如何解决这个问题,你应该澄清一下Numbers.txt的格式。

当你读到这个循环的内部,.nextDouble(),你可能已经在最后一个元素,所以这个循环将失败,例如,如果文件中的最后一个double在它后面没有换行符。

+0

谢谢@markspace数字txt。包含超过2000个数字。每个号码从一个新行开始。 –

+0

每个数字*结尾*换行吗?正如我所提到的那样,该文件中的最后一项可能会让你感到困惑。 – markspace

+0

我不太清楚我的理解。 –

相关问题