0

我正在尝试对对象数组列表执行二进制搜索。用户必须输入管理员号码才能执行搜索。这是我的代码:

文件控制器类从 .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 方法和我的二进制搜索。因为当我删除它们时,我的程序可以很好地获取数组列表。仅当我尝试对我的对象列表执行搜索时才会出现该错误。任何帮助,将不胜感激。

提前致谢。

4

1 回答 1

0

正如错误消息所说,异常发生在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可能没有;您在构造函数中阅读的那么多标记(由 分隔)。

于 2013-06-10T04:38:44.950 回答