-4

This is the error I get when I run StatsDemo. The program should get no output to the console, but running the program will create a file called Results.txt with your output. The output you should get at this point is: You should get a mean of 77.444 and standard deviation of 10.021.

run StatsDemo.

This program calculates statistics on a file containing a series of numbers

Enter the file name: [DrJava Input Box]

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)

Here is the code:

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");
    }
}
Kurt Van den Branden
  • 11,995
  • 10
  • 76
  • 85
  • 4
    Lack of a compiler error does not mean that the program is bug free, and in fact your program is not able to handle the input you give it. Without code though we're at a loss to be able to help you. – Hovercraft Full Of Eels Nov 20 '16 at 22:05
  • Because you did something wrong. – Timothy Truckle Nov 20 '16 at 22:05
  • Because it's a runtime error. You closed the scanner, maybe? No line was found... Hard to say without your code – OneCricketeer Nov 20 '16 at 22:06
  • Please provide code so we can help you. The exception you are showing is a runtime exception. The fact that you see DrJava compiler in the stacktrace means most likely it is somewhat started from the IDE. If you start it with java from commandline it will begin with your `main()` method. – eckes Nov 20 '16 at 22:07
  • This kind of error is runtime error. We need to see your code in order to help you. – Rafal Nov 20 '16 at 22:07
  • @Rafal I've added the code. – Kelly Williams Nov 20 '16 at 22:27

1 Answers1

0

Your error appears to be coming from this line:

inputFile.nextLine(); 

I'm guessing that your input file, Numbers.txt, has no newlines beyond the point you are currently reading.

Here's the loop:

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

.hasNext() by default looks for any white space, not just lines, so you could enter this loop with no newlines available. You should clarify for us what the format of Numbers.txt is if you don't understand how to fix this loop.

When you read the inside this loop, .nextDouble(), you could be already at the last element, so this loop will fail if for example the last double in the file doesn't have a newline after it.

markspace
  • 10,621
  • 3
  • 25
  • 39