简体   繁体   中英

Cant return object of multiple types (java)

I am trying to return an object to an ArrayList, but the object is of multiple types (String and int). Instead of returning the object, the program stops running. enterTeacher() returns teacher just fine since its made of only Strings. enterStudents() however stops the program after inputting the grade. I think its because students is made of both Strings and ints. I tried creating an array of student objects, but I couldn't return the array.

public class PrintReport {
    Scanner in = new Scanner(System.in);
    char answer;
    ArrayList<Displayable> list = new ArrayList<Displayable>();

    public PrintReport() {
        System.out.println("First You Need To Create A Classroom");
        do {
            Displayable c = enterClassroom();
            list.add(c);
            System.out.print("Enter another class room (Y or N)? ");
            answer = in.next().toUpperCase().charAt(0);
            in.nextLine();
        } while (answer != 'N');
        report(list);
    }

    public Displayable enterClassroom() {
        ArrayList<Displayable> studentList = new ArrayList<Displayable>();
        System.out.print("Enter Room Number: ");
        int room = in.nextInt();
        in.nextLine();
        System.out.println("Now you need to enter a teacher for the classroom.");
        Displayable teacher = enterTeacher();
        System.out.println("Now you need to Add Students for the classroom.");
        Displayable Student = enterStudents();
        do {
        } while (answer != 'N');
        return new Classroom(room, teacher, studentList);
    }

    public Displayable enterTeacher() {
        System.out.print("Enter first name of teacher: ");
        String fName = in.nextLine();
        System.out.print("Enter last name of teacher: ");
        String lName = in.nextLine();
        System.out.print("Enter subject of teacher: ");
        String subject = in.nextLine();
        Displayable teacher = new Teacher(fName, lName, subject);
        return teacher;
    }

    public Displayable enterStudents() {
        int studentID;
        System.out.print("Enter student id(greater than 0): ");
        studentID = Integer.parseInt(in.nextLine());
        System.out.print("Enter first name of student: ");
        String fName = in.nextLine();
        System.out.print("Enter last name of student: ");
        String lName = in.nextLine();
        int grade = 0;
        System.out.print("Enter final grade of student(0-100): ");
        grade = Integer.parseInt(in.nextLine());
        Displayable Student = new Student(fName, lName, studentID, grade);
        return Student;
    }

    void report(ArrayList<Displayable> list) {
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i).display());
        }

    }

    public static void main(String[] args) {
        PrintReport pr = new PrintReport();
    }
}
 do { } while (answer;= 'N');

This will infinite loop.

think its because students is made of both Strings and ints

Not the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM