【发布时间】:2015-04-21 17:17:15
【问题描述】:
我正在尝试对对象数组列表执行二进制搜索。用户必须输入管理员号码才能执行搜索。这是我的代码:
文件控制器类从 .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;
}
}
可执行的 main 方法在这里,在 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.readStudent("student.txt");然后它在 Student 构造函数中进行内部拆分。
-
@loki 那么我应该怎么做才能解决它呢?你能给我一些代码示例,比如如何解决它吗?我真的不明白你的意思
标签: java eclipse arraylist binary-search compareto