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

すべてのセッターとゲッター、および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 に答える