title: Run Error in Java...Scanner closed?
body: <p>So I have this code and it's supposed to read data from a file called "Numbers.txt". In this file, it is simply just 2003 lines of decimal numbers. The goal of the program is to read this file and make calculations. First I had to get the mean, which I've managed to do, but the problem is I have to now go back in and get the standard deviation. It compiles fine but I get a run error as below:</p>
  
  <pre><code>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>This is 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>
  
  <p>I'm not sure why I'm getting this error. I got a similar (but not the same) one from the mean calculation earlier, but the resolve for that doesn't work for this. Any ideas?</p>
  
  <p><strong>After changing to inputFile2...</strong></p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalStateException: Scanner closed
  at java.util.Scanner.ensureOpen(Scanner.java:1115)
  at java.util.Scanner.next(Scanner.java:1510)
  at java.util.Scanner.nextDouble(Scanner.java:2456)
  at StatsDemo.main(StatsDemo.java:51)
  </code></pre>