簡體   English   中英

Java中對象數組列表的BinarySearch

[英]BinarySearch on Object Array List in Java

我正在嘗試在對象數組列表上執行二進制搜索。 用戶必須輸入管理員編號才能執行搜索。 這是我的代碼:

文件控制器類,用於從.text文件讀取文件

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

具有所有setter和getter和compareTo方法的學生類

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

可執行的主要方法在此處,在StudentSearch類中

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

我想通過使用用戶輸入的管理員編號來執行搜索。 但是,這是錯誤消息:

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)

我認為問題出在compareTo方法和我的二進制搜索中。 因為當我刪除它們時,我的程序可以很好地獲取數組列表。 僅當我嘗試在對象列表上執行搜索時,才會發生該錯誤。 任何幫助,將不勝感激。

提前致謝。

如錯誤消息所述,該異常是在Student構造函數中發生的-

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

您在這里調用構造函數-

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

在構造函數中讀取時, searchAdminNo可能沒有那么多的標記(由;分隔)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM