2016-11-20 73 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

我不太清楚我的理解。 –

相關問題