title: Can't understand how to solve noSuchElementException?
body: <p>I'm trying to write a program to read a file called <code>scores.txt</code> where it prints out the ID of the student with the highest average across their courses along with their student ID.</p>
  
  <p>This is what <code>scores.txt</code> looks like:</p>
  
  <pre><code>34     c081 c082 c083 c084
  S2023  99   75   85   62
  S2025  -1   92   67   52
  S1909  100  83   45   -1
  
  </code></pre>
  
  <p>So basically, the <code>34</code> a the beginning is a 3 for the number of students and a 4 for the number of courses (yes, I know this is silly, but the file was provided for my task). The c numbers such as c081 are course codes at the school, and the s numbers such as s2023 are student numbers. The figures in the middle represent their scores, the -1 also means they weren't enrolled in the unit.</p>
  
  <p>Anyway, so far I've written a <code>MySchool</code> class and a <code>Student</code> class, which I'll paste below:</p>
  
  <pre><code>import java.io.File;
  import java.io.FileNotFoundException; 
  import java.util.ArrayList;
  import java.util.Scanner; 
  
  public class MySchool {
      public static void main(String[] args) {
          int numberOfStudents;
          ArrayList&lt;Student&gt; allStudents = new ArrayList&lt;Student&gt;() ;
  
          // grab first line only, and check if first target is integer, or string
          try {
  
              File scoresFile = new File("Scores.txt");
              Scanner scoresFileReader = new Scanner(scoresFile);
  
              String headerRow = scoresFileReader.nextLine();
              numberOfStudents = headerRow.charAt(0);
  
              while(scoresFileReader.hasNextLine()) {
              for (int studentI = 0; studentI &lt; numberOfStudents; studentI++) {
                  String studentText = scoresFileReader.nextLine();
                  System.out.println(studentText);
                  Student student = new Student(studentText);
                  allStudents.add(student);
              }}
  
              scoresFileReader.close();
  
          } catch (FileNotFoundException e) {
              System.out.println("An error occurred");
              e.printStackTrace();
          }
  
          float highestAverage = 0;
          String highestScoringStudentNumber = null;
  
          for (Student student : allStudents) {
              if (student.getAverageScore() &gt; highestAverage) {
                  highestAverage = student.getAverageScore();
                  highestScoringStudentNumber = student.getStudentNumber();
              }
  
          }
          System.out.println("Highest scoring student: " + highestScoringStudentNumber);
      }
  }
  </code></pre>
  
  <pre><code>import java.util.ArrayList;
  
  public class Student {
  
      private String studentNumber;
      private ArrayList&lt;Integer&gt; scores = new ArrayList&lt;Integer&gt;();
  
      public Student(String studentText) {
          String[] parts = studentText.split(studentText, ' ');
          this.studentNumber = parts[0];
  
          for (int i = 1; i &lt; parts.length - 1; i++) {
              scores.add(Integer.parseInt(parts[i + 1]));
          }
      }
  
      public String getStudentNumber() {
          return this.studentNumber;
      }
  
      public float getAverageScore() {
          int sum = 0;
          for (int i = 0; i &lt;this.scores.size(); i++) {
              sum += this.scores.get(i);
          }
          return sum / this.scores.size();
      }
  
  
  }
  </code></pre>
  
  <p>Basically I want to be able to have an object for students where they have their student number and their scores. This is so that I can give them an average.</p>
  
  <p>However, it seems that I've done something wrong in reading the file in (Haven't ever done this before), because the <code>String studentText = scoresFileReader.nextLine();</code> line throws me an error that states : <code>Exception in thread "main" java.util.NoSuchElementException: No line found
      at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
      at MySchool.main(MySchool.java:22)</code></p>
  
  <p>If it's any help, the three student codes and their scores print out the way they should before I get this error. </p>
  
  <p>Can anyone help me to get this up and running? I'm not sure how to resolve it</p>
  
  <p>EDIT:</p>
  
  <p>I've actually noticed the issue to be that somehow <code>numberOfStudents</code> is being set to 51 when there's no 51 in the data. Even if I run the code below, it prints the first value of <code>headerRow</code> confirming that it is 3, which is correct. The when I use the same code to assign it to <code>numberOfStudents</code> suddenly it's become 51 when it prints again?</p>
  
  <pre><code>import java.io.File; // Import the File class
  import java.io.FileNotFoundException; // Import this class to handle errors
  import java.util.ArrayList;
  import java.util.Scanner; // Import the Scanner class to read text files
  
  public class MySchool {
      public static void main(String[] args) {
          int numberOfStudents;
          ArrayList&lt;Student&gt; allStudents = new ArrayList&lt;Student&gt;() ;
  
          // grab first line only, and check if first target is integer, or string
          try {
  
              File scoresFile = new File("Scores.txt");
              Scanner scoresFileReader = new Scanner(scoresFile);
  
  
              String headerRow = scoresFileReader.nextLine();
  
              System.out.println(headerRow.charAt(0));
  
              numberOfStudents = headerRow.charAt(0);
  
              System.out.println(numberOfStudents);
  
  
              for (int studentI = 0; studentI &lt; numberOfStudents; studentI++) {
                  String studentText = scoresFileReader.nextLine();
                  System.out.println(studentText);
                  Student student = new Student(studentText);
                  allStudents.add(student);
              }
  
              scoresFileReader.close();
  
          } catch (FileNotFoundException e) {
              System.out.println("An error occurred");
              e.printStackTrace();
          }
  
          float highestAverage = 0;
          String highestScoringStudentNumber = null;
  
          for (Student student : allStudents) {
              if (student.getAverageScore() &gt; highestAverage) {
                  highestAverage = student.getAverageScore();
                  highestScoringStudentNumber = student.getStudentNumber();
              }
  
          }
          System.out.println("Highest scoring student: " + highestScoringStudentNumber);
      }
  }
  </code></pre>