title: Scanner detect end of file Java
body: <p>I am trying to read in data from a text file and inserting it into an array, one line gap in the file means that a new array will be created. Two lines and the scanner should be closed. Here is the text file:</p>
  
  <pre><code>`Test
  test
  test
  Telephone
  Cardiology
  
  Test Doctor 2
  Test
  20 Test
  Telephone
  Cardiology
  `
  
  
    public Object[][] readData() throws FileNotFoundException{
    Scanner sc = new Scanner(new File("DoctorRecordsFile.txt"));
    String line;
    int emptyFile = 0 ;
  
    int i = 0;
    int x = 0;
    String[][] doctor = new String[100][5];
    while (emptyFile &lt;= 1){
    line = sc.nextLine() ;
  
    if ((!line.isEmpty())){
          doctor[i][x] = line;
          System.out.println(doctor[i][x]);
  
          x++;
          emptyFile = 0;   
    }
    else{
        System.out.println("PRE call emptyFile: " + emptyFile);
         emptyFile++;
        System.out.println("Post call emptyFile: " + emptyFile);
      if(emptyFile &gt;= 2){
         sc.close();
      }
      else{
         i++;
         x = 0;
      }
      }
    }
    return new Object[][]{doctor[0]};
  </code></pre>
  
  <p>The actual result I expect is that the scanner will close and stop reading in data, here are my console errors: </p>
  
  <pre><code>at java.util.Scanner.nextLine(Scanner.java:1540)
      at medicalsystem.ReadInDoctors.readData(ReadInDoctors.java:35)
      at medicalsystem.MainMenu.View_Doctor_ButtonActionPerformed(MainMenu.java:193)
  </code></pre>