0

我的代码全部编译,但在运行驱动程序时我不断收到错误消息。错误是

线程“主”中的异常java.util.InputMismatchException

    at java.util.Scanner.throwFor(Scanner.java:909)
    at java.util.Scanner.next(Scanner.java:1530)
    at java.util.Scanner.nextInt(Scanner.java:2160)
    at java.util.Scanner.nextInt(Scanner.java:2119)
    at University.readFromFile(University.java:174)
    at TestDriver2.main(TestDriver2.java:8)

我的代码主体是:

//read from file
public static University readFromFile(String filename) throws EmptyFileException {
File file = new File(filename);
University university = null;
Scanner in = null;
try {
    in = new Scanner(file);
    String str = in.nextLine();
    Scanner in2 = new Scanner(str).useDelimiter(":");
    in2.next(); 
    String uName = in2.next();

    //Read University info
    str = in.nextLine();
    in2 = new Scanner(str).useDelimiter(":");
    in2.next(); 
    String uTerm = in2.next();
    str = in.nextLine();
    in2 = new Scanner(str).useDelimiter(":");
    in2.next(); 
    int uYear = in2.nextInt();
    str = in.nextLine();
    in2 = new Scanner(str).useDelimiter(":");
    in2.next(); 
    int uNumStudents = in2.nextInt();
    str = in.nextLine();
    in2 = new Scanner(str).useDelimiter(":");
    in2.next(); 
    int uNumInstructors = in2.nextInt();
    str = in.nextLine();
    in2 = new Scanner(str).useDelimiter(":");
    in2.next(); 
    int uNumCourses = in2.nextInt();

    //Create and read arrays of objects
    Student[] uStudents = new Student[uNumStudents];
    for(int i = 0; i < uNumStudents; i++)
                    uStudents[i] = readStudent(in);

    Instructor[] uInstructors = new Instructor[uNumInstructors];
    for(int i = 0; i < uNumInstructors; i++)
                    uInstructors[i] = readInstructor(in);

                //Create array of possible TAs to pass into readCourse method
                GradStudent[] gStudents = new GradStudent[uNumStudents];
                for(int j=0; j<uStudents.length; j++) {
                    for(int i=0; i<uStudents.length; i++) {
                        if(uStudents[i] instanceof GradStudent)
                            gStudents[j] = (GradStudent) uStudents[i];
                    }
                } 

    Course[] uCourses = new Course[uNumCourses];
    for(int i = 0; i < uNumCourses; i++)
                    uCourses[i] = readCourse(in, uInstructors, gStudents, uStudents);

    //Create University object
    university = new University(uName, uTerm, uYear, uStudents, uInstructors, uCourses);
    return university;

} catch (FileNotFoundException fe) {
    fe.printStackTrace();
    return null;
}

异常文件:

public class EmptyFileException extends Exception {
    String filename;
    EmptyFileException() { }

    EmptyFileException(String filename) {
        this.filename = filename;
    }

    public String toString() {
        return "The file " + filename + " is empty.";
    }
}

还有司机:

public class TestDriver2 {

public static void main(String[] args) throws EmptyFileException {

    University ufl = University.readFromFile("university.txt");

            if (ufl != null) {
                System.out.println("\n--Printing out University info--");
                System.out.println(ufl);

                System.out.println("\n--Printing out Students info--");
                Student[] students = ufl.getStudents();
                for (Student s : students)
                        System.out.println("\n" + s);
            }
    }
}
4

0 回答 0