Java无法打印出阵列列表?

时间:2015-09-16 20:33:54

标签: java arrays arraylist java.util.scanner

所以我有3个类,一个运行所有代码。学生班,研究生班和本科班,以及运行代码的实验室4班。该代码询问有多少研究生和有多少本科学生,然后要求用户输入每个学生的所有信息。在输入所有信息之后,将under grad或grad student对象添加到学生的数组列表中。添加完所有学生后,打印所有信息。但是,当我输入所有信息时,阵列不打印,程序终止。如何让阵列打印出来?

代码:

实验4:创建学生对象,数组列表,将它们添加到数组中,并打印添加到数组列表中的对象的所有信息

import java.util.ArrayList;
import java.util.Scanner;

public class Lab04 {

public static void main(String[] args) {
    ArrayList <Student> studentList = new ArrayList <Student>();

    Scanner s = new Scanner(System.in);
    System.out.println("How many Graduate Students do you want to store?");
    int numOfStudents = s.nextInt();
    s.nextLine();
    for (int i = 0; i < numOfStudents; i++) {
        System.out.println("What is the student's name?");
        String name = s.nextLine();
        System.out.println("What is the student's GPA?");
        double GPA = s.nextDouble();
        s.nextLine();
        System.out.println("What is the student's ID?");
        String ID = s.nextLine();
        System.out.println("What is the student's High School?");
        String highSchool = s.nextLine();
        System.out.println("What is the student's graduate major?");
        String gradMajor = s.nextLine();
        System.out.println("What is the student's undergraduate major?");
        String underMajor = s.nextLine();
        System.out.println("What is the student's undergradute school? ");
        String underSchool = s.nextLine();
        GraduateStudent gradStu = new GraduateStudent(name, GPA, ID, highSchool, gradMajor, underMajor,
                underSchool);
        studentList.add(gradStu);
    }
    System.out.println("How many UnderGraduate students are there?");
    int numOfUnder = s.nextInt();
    s.nextLine();
    for (int j = 0; j < numOfUnder; j++) {
        System.out.println("What is the student's name?");
        String name = s.nextLine();
        System.out.println("What is the student's GPA?");
        double GPA = s.nextDouble();
        s.nextLine();
        System.out.println("What is the student's ID?");
        String ID = s.nextLine();
        System.out.println("What is the student's High School?");
        String highSchool = s.nextLine();
        System.out.println("What is the student's major?");
        String major = s.nextLine();
        System.out.println("What the student's minor?");
        String minor = s.nextLine();
        UnderGraduate UnderGrad = new UnderGraduate(name, GPA, ID, highSchool, major, minor);
        studentList.add(UnderGrad);
    }
    for (int j = 0; j < studentList.size(); j++) {
        (studentList.get(j)).getStudentInformation();
    }
}

}

学生班:

public class Student {

private String name;
private double gpa;
private String id;
private String highSchool;
//private String major;
//private String minor;

public Student() {
    name = "";
    gpa = 0.0;
    id = "";
    highSchool = "";
    //major = "";
    //minor = "";
}
public Student(String name, double gpa, String id, String highSchool){
    this.name = name;
    this.gpa = gpa;
    this.id = id;
    this.highSchool = highSchool;

}



public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public double getGpa() {
    return gpa;
}

public void setGpa(double gpa) {
    this.gpa = gpa;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getHighSchool() {
    return highSchool;
}

public void setHighSchool(String highSchool) {
    this.highSchool = highSchool;
}


public String getStudentInformation() {
    String result = "Name: " + name + " GPA: " + gpa + " ID: " + id
            + " High School: " + highSchool;
    return result;
}

}

研究生班:

public class GraduateStudent extends Student {

private String gradMajor;
private String underMajor;
private String underSchool;

public GraduateStudent(String name, double gpa, String id, String highSchool,
        String gradMajor, String underMajor, String underSchool) {
super(name, gpa, id, highSchool);
this.gradMajor = gradMajor;
this.underMajor = underMajor;
this.underSchool = underSchool;
}
public String getGradMajor() {
    return gradMajor;
}

public void setGradMajor(String gradMajor) {
    this.gradMajor = gradMajor;
}

public String getUnderMajor() {
    return underMajor;
}

public void setUnderMajor(String underMajor) {
    this.underMajor = underMajor;
}
public String getUnderSchool() {
    return underSchool;
}

public void setUnderSchool(String underSchool) {
    this.underSchool = underSchool;
}

@Override
public String getStudentInformation() {
    String result = super.getStudentInformation()+
            "Graduate Major: " + gradMajor + "Undergraduate Major: " + underMajor +
            "Undergraduate School: " + underSchool;
    return result;
}
}

2 个答案:

答案 0 :(得分:5)

因为你没有打印任何东西。变化

for (int j = 0; j < studentList.size(); j++) {
    (studentList.get(j)).getStudentInformation();
}

for (int j = 0; j < studentList.size(); j++) {
    System.out.println((studentList.get(j)).getStudentInformation());
}

答案 1 :(得分:1)

当您询问getStudentInformation时,它返回一个String,您现在要做的是System.out.println(...)此String对象