我正在尝试在对象数组列表上执行二进制搜索。用户必须输入管理员号码才能执行搜索。这是我的代码:
从.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;
}
学生班与所有的二传手和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 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);
}
我想使用用户输入管理员号码执行搜索。但是,这是错误消息:
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方法和我的二进制搜索。因为当我删除它们时,我的程序可以使数组列表正常。只有当我尝试在对象列表上执行搜索时才会出现此错误。任何帮助将不胜感激。
提前致谢。
答案 0 :(得分: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
可能没有那么多令牌(由;
分隔)。