Search code examples
javaeclipsearraylistbinary-searchcompareto

BinarySearch on Object Array List in Java


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 :

File Controller class to read file from .text file

public class FileController extends StudentApp{
private String fileName;

public FileController() {
    String fileName = "student.txt";
}

public FileController(String fileName) {
    this.fileName = fileName;
}

public ArrayList<String> readLine() {
    ArrayList<String> studentList = new ArrayList<String>();
    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;
}

Student Class with all the setter & getter and compareTo method

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<Student> readStudent(String file) {
    FileController fc = new FileController(file);
    ArrayList<Student> recs = new ArrayList<Student>();
    ArrayList<String> recsReturn = new ArrayList<String>();

    recsReturn = fc.readLine();

    for (int index = 0; index < 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;
    }
}

Executable main method lies here, in StudentSearch Class

public static void main(String [] args){
    Scanner sc = new Scanner(System.in);
    ArrayList <Student> studentList = new ArrayList <Student> ();
    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);
}

I want to perform search by using the user input admin number. However, here's the error message:

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at StudentGradeApps.Student.<init>(Student.java:22)
at StudentGradeApps.StudentSearch.main(StudentSearch.java:14)

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.

Thanks in advance.


Solution

  • As the error message say, the exception is occurring within the Student constructor -

    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();
    }
    

    You are invoking the constructor here -

    int pos = Collections.binarySearch(studentList, new   Student(searchAdminNo));
    

    searchAdminNo probably doesn't have that many tokens (delimited by ;) as you are reading in the constructor.