title: Why wont Java read these lines from a file?
body: <p>Compile and everything is fine but when I try to run the program and input the file for the output, it says it cant read my file. I have a series of numbers stored individually on seperate lines in a file called "Numbers.txt" and it is supposed to take each one by the line and end up calculating the mean and then write the mean to a file called "Results.txt"</p>
  
  <p>Here is there error:</p>
  
  <pre><code> ----jGRASP exec: java StatsDemo
  
  This program calculates statisticson a file containing a series of numbers
  Enter the file name:  Results.txt
  Exception in thread "main" java.util.NoSuchElementException: No line found
      at java.util.Scanner.nextLine(Scanner.java:1585)
      at StatsDemo.main(StatsDemo.java:39)
  
  ----jGRASP wedge2: exit code for process is 1.
  ----jGRASP: operation complete.
  </code></pre>
  
  <p>What I have for code is:</p>
  
  <pre><code>File rf = new File("Numbers.txt"); //Create a FileReader object passing it the filename
      Scanner inputFile = new Scanner(rf); //Create a BufferedReader object passing it the FileReader object.
      //priming read to read the first line of the file
      while (inputFile.hasNext()) //create a loop that continues until you are at the end of the file
      {   
       sum += inputFile.nextDouble(); //convert the line into a double value and add the value to the sum
          count ++; //increment the counter
          inputFile.nextLine(); //read a new line from the file
    }
      inputFile.close(); //close the input file
      mean = sum/count; //store the calculated mean
  </code></pre>
  
  <p>I can post more of the program if necessary. I tried searching but I couldn't find an answer to why it wont read the line.</p>
  
  <p><strong>Update: I moved on and did a Standard Deviation problem. I get a similar error even though I used the new technique that worked previously.</strong></p>
  
  <p>Error: </p>
  
  <pre><code>This program calculates statisticson a file containing a series of numbers
  Enter the file name:  Results.txt
  Exception in thread "main" java.lang.IllegalStateException: Scanner closed
      at java.util.Scanner.ensureOpen(Scanner.java:1115)
      at java.util.Scanner.hasNext(Scanner.java:1379)
      at StatsDemo.main(StatsDemo.java:49)
  </code></pre>
  
  <p>My code:</p>
  
  <pre><code>        File rf2 = new File("Numbers.txt"); //reconnect to the FileReader object passing it the filename
      Scanner inputFile2 = new Scanner(rf2);//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
      //priming read to read the first line of the file
      while (inputFile.hasNext()) //loop that continues until you are at the end of the file
      {
       difference = inputFile.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 (inputFile.hasNextDouble())
          inputFile.nextLine(); //read a new line from the file
      }
    inputFile.close(); //close the input file
      stdDev = Math.sqrt(sum/count); //store the calculated standard deviation  
  </code></pre>