title: Reading from File - Error "Exception in thread "main" java.util.NoSuchElementException: No line found"
body: <p>I am writing a program that reads from a file titled &quot;grades.txt&quot; and displays the student's name, three grades, and the average of those three grades.</p>
  <p>The text file looks like this:</p>
  <pre class="lang-none prettyprint-override"><code>Bobby
  Doe
  65
  65
  65
  
  Billy
  Doe
  100
  100
  95
  
  James
  Doe
  85
  80
  90
  </code></pre>
  <p>Here is the code. I am able to read from the file and output everything correctly.</p>
  <pre><code>import java.util.Scanner; // Needed for Scanner class.
  import java.io.*;          // Needed for I/O class.
  
  public class TestScoresRead
  {
     public static void main(String[] args) throws IOException
     {
  
        // Open the file
        File file = new File(&quot;Grades.txt&quot;);
        Scanner inputFile = new Scanner(file);
        
        // Read lines from the file
        while (inputFile.hasNext())
        {
           String firstName = inputFile.next();
           String lastName = inputFile.next();
           double grade1 = inputFile.nextDouble();
           double grade2 = inputFile.nextDouble();
           double grade3 = inputFile.nextDouble();
           String nextLine = inputFile.nextLine();
           
           int total = (int)grade1 + (int)grade2 + (int)grade3;
           int average = total / 3;
           
           
           System.out.println(&quot;Name: \t&quot; + firstName + &quot; &quot; + lastName);
           System.out.println(&quot;Test 1:\t&quot; + grade1);
           System.out.println(&quot;Test 2: \t&quot; + grade2);
           System.out.println(&quot;Test 3: \t&quot; + grade3);
           System.out.println(&quot;&quot;);
           System.out.println(&quot;Average: &quot; + average);
           
            if (average &lt; 60)
              System.out.println(&quot;Grade : \t F&quot;);
        
            else if (average &lt; 70)
              System.out.println(&quot;Grade : \t D&quot;);
        
            else if (average &lt; 80)
              System.out.println(&quot;Grade: \t C&quot;);
        
            else if (average &lt;90)
            System.out.println(&quot;Grade: \t B&quot;);
        
            else
           System.out.println(&quot;Grade: \t A&quot;);
           
           System.out.println(&quot;&quot;);
  
        }
        inputFile.close();
      }
  }
  </code></pre>
  <p>However, I keep getting this error and I am not sure why:</p>
  <pre><code>Exception in thread &quot;main&quot; java.util.NoSuchElementException: No line found
  at java.util.Scanner.nextLine(Scanner.java:1540)
  at TestScoresRead.main(TestScoresRead.java:21)
  </code></pre>
  <p>From the research I've done, I believe it has something to do with going from <code>nextLine</code> to <code>nextDouble</code>, and the <code>\n</code> being stuck in the keyboard buffer.</p>
  <p>Or maybe I'm not using <code>hasNext</code> right?</p>
  <p>How can I fix the error?</p>