有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么在运行程序时,即使没有编译错误,也会出现此错误?

这是我在运行StatsDemo时遇到的错误。该程序不应向控制台获取任何输出,但运行该程序将创建一个名为Results的文件。txt与您的输出。此时您应该得到的输出是:您应该得到77.444的平均值和10.021的标准偏差

运行StatsDemo

此程序计算包含一系列数字的文件的统计信息

输入文件名:[DrJava输入框]

java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at StatsDemo.main(StatsDemo.java:34) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at 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");
    }
}

共 (1) 个答案

  1. # 1 楼答案

    您的错误似乎来自此行:

    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后面没有换行符,则此循环将失败