title: Keep getting the java.util.NoSuchElementException on this program
body: <p>I am currently writing a program that writes class codes to a file and then reads them back from said file and prints them to the screen.  Everytime I run the program I keep getting the java.util.NoSuchElementException.  This problem persists after every modification I have made and I'm not sure where to go from here.  Any help would be greatly appreciated.</p>
  
  <pre><code>package u2a1_readtextfilehandleexcep;
  import java.io.*;
  import java.io.FileNotFoundException;
  import java.io.IOException;
  import java.util.Scanner;
  
  public class U2A1_ReadTextFileHandleExcep {
  
  public static void main(String[] args) throws FileNotFoundException, IOException {
      //Create new file called course.txt
      java.io.File file = new java.io.File("course.txt");
      if (file.exists()) {
          System.out.println("File already exists; try another name.");
          System.exit(0);
      }
      //Input the specified words to the file
      try (PrintWriter output = new PrintWriter(file)) {
          output.println("IT2249 6 Introduction to Programming with Java");
          output.println("IT2230 3 Introduction to Database Systems");
          output.println("IT4789 3 Mobile Cloud Computing Application Development");
      }
      try (
              //Reads from file to the console
              Scanner input = new Scanner(file)) {
              while (file.canRead()) {
              String code = input.next();
              int creditHours = input.nextInt();
              String courseTitle = input.nextLine();
              System.out.println("Course Code = " + code + " | Credit Hours = " + creditHours + " | Course Title = " + courseTitle);
          }
           input.close();   
      }
  }
  
  }
  </code></pre>
  
  <p>And after running the program:</p>
  
  <pre><code>Course Code = IT2249 | Credit Hours = 6 | Course Title =  Introduction to Programming with Java
  Exception in thread "main" java.util.NoSuchElementException
  Course Code = IT2230 | Credit Hours = 3 | Course Title =  Introduction to Database Systems
  Course Code = IT4789 | Credit Hours = 3 | Course Title =  Mobile Cloud Computing Application Development
  at java.util.Scanner.throwFor(Scanner.java:862)
  at java.util.Scanner.next(Scanner.java:1371)
  at u2a1_readtextfilehandleexcep.U2A1_ReadTextFileHandleExcep.main(U2A1_ReadTextFileHandleExcep.java:26)
  C:\Users\Deb\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
  BUILD FAILED (total time: 1 second)
  </code></pre>