title: BinarySearch on Object Array List in Java
body: <p>I am trying to perform binary search on an object array list. The user must type in admin number to perform search. Here is my code :</p>
  
  <p><strong>File Controller class to read file from .text file</strong></p>
  
  <pre><code>public class FileController extends StudentApp{
  private String fileName;
  
  public FileController() {
      String fileName = "student.txt";
  }
  
  public FileController(String fileName) {
      this.fileName = fileName;
  }
  
  public ArrayList&lt;String&gt; readLine() {
      ArrayList&lt;String&gt; studentList = new ArrayList&lt;String&gt;();
      try {
          FileReader fr = new FileReader(fileName);
          Scanner sc = new Scanner(fr);
          while (sc.hasNextLine()) {
              studentList.add(sc.nextLine());
          }
          fr.close();
      } catch (FileNotFoundException exception) {
          System.out.println("File " + fileName + " was not found");
      } catch (IOException exception) {
          System.out.println(exception);
      }
      return studentList;
  }
  </code></pre>
  
  <p><strong>Student Class with all the setter &amp; getter and compareTo method</strong></p>
  
  <pre><code>public Student(String adminNo, String name, GregorianCalendar birthDate) {
      this.adminNo = adminNo;
      this.name = name;
      this.birthDate = birthDate;
  }
  
  public Student(String record) {
      Scanner sc = new Scanner(record);
      sc.useDelimiter(";");
      adminNo = sc.next();
      name = sc.next();
      birthDate = MyCalendar.convertDate(sc.next());
      test1 = sc.nextInt();
      test2 = sc.nextInt();
      test3 = sc.nextInt();
  }
  public String toString(){
      return (adminNo + " " + name + " " + MyCalendar.formatDate(this.birthDate));
  }
  
  public static ArrayList&lt;Student&gt; readStudent(String file) {
      FileController fc = new FileController(file);
      ArrayList&lt;Student&gt; recs = new ArrayList&lt;Student&gt;();
      ArrayList&lt;String&gt; recsReturn = new ArrayList&lt;String&gt;();
  
      recsReturn = fc.readLine();
  
      for (int index = 0; index &lt; recsReturn.size(); index++) {
          String input = recsReturn.get(index);
          recs.add(new Student(input));
      }
  
      return recs;
  }
  
  
  public int compareTo(Student s) {
      return Compare(this, s);
  }
  
  public int Compare(Student s1, Student s2){
      if(s1.getAdminNo().equals(s2.getAdminNo())) {
        return 0;
      }else{
        return 1;
      }
  }
  </code></pre>
  
  <p><strong>Executable main method lies here, in StudentSearch Class</strong></p>
  
  <pre><code>public static void main(String [] args){
      Scanner sc = new Scanner(System.in);
      ArrayList &lt;Student&gt; studentList = new ArrayList &lt;Student&gt; ();
      studentList = Student.readStudent("student.txt");
      Collections.sort(studentList);
  
      System.out.println("Enter student admin number: ");
      String searchAdminNo = sc.next();
  
      int pos = Collections.binarySearch(studentList, new   Student(searchAdminNo));
      System.out.println(pos);
  }
  </code></pre>
  
  <p>I want to perform search by using the user input admin number. However, here's the error message: </p>
  
  <pre><code>Exception in thread "main" java.util.NoSuchElementException
  at java.util.Scanner.throwFor(Unknown Source)
  at java.util.Scanner.next(Unknown Source)
  at StudentGradeApps.Student.&lt;init&gt;(Student.java:22)
  at StudentGradeApps.StudentSearch.main(StudentSearch.java:14)
  </code></pre>
  
  <p>I think the problem lies at the compareTo method and my binary search. Because when I removed them, my program can get the array list fine. The error only occurs when I try to perform search on my object list. Any help would be appreciated.</p>
  
  <p>Thanks in advance.</p>