gpt4 book ai didi

java - 是否可以通过此方法返回或以某种方式访问​​ "courses"中的数据?

转载 作者:行者123 更新时间:2023-12-01 17:30:58 26 4
gpt4 key购买 nike

我正在做我的第一个 Java 编程作业,我还有另一个问题。我将 Course[] 放在 Student[] 内,但现在似乎遇到 NullPointerException 错误,我不明白为什么。

public Student[] analyzeData() {
Scanner inputStream = null;

try {
inputStream = new Scanner(new FileInputStream("Programming Assignment 1 Data.txt"));
} catch (FileNotFoundException e) {
System.out.println("File Programming Assignment 1 Data.txt could not be found or opened.");
System.exit(0);
}

int numberOfStudents = inputStream.nextInt();
int tuitionPerHour = inputStream.nextInt();

Student[] students = new Student[numberOfStudents];
for (int i = 0; i < numberOfStudents; i++) {
String firstName = inputStream.next();
String lastName = inputStream.next();
int studentID = inputStream.nextInt();
String isTuitionPaid = inputStream.next();
int numberOfCourses = inputStream.nextInt();


Course[] courses = new Course[numberOfCourses];
for (i = 0; i < numberOfCourses; i++) {
String courseName = inputStream.next();
String courseNumber = inputStream.next();
int creditHours = inputStream.nextInt();
String grade = inputStream.next();
Course currentCourse = new Course(courseName, courseNumber, creditHours, grade);
courses[i] = currentCourse;
}
Student currentStudent = new Student(firstName, lastName, studentID, isTuitionPaid, numberOfCourses, courses);
students[i] = currentStudent;
}
return students;
}

输入文件的格式为:

3 345
Lisa Miller 890238 Y 2
Mathematics MTH345 4 A
Physics PHY357 3 B


Bill Wilton 798324 N 2
English ENG378 3 B
Philosophy PHL534 3 A

其中courses 包含有关类(class)的信息,students 包含有关学生的信息。

最佳答案

文件的自然对象映射将是一个 Student 对象列表,每个对象都包含一个 Course 对象列表。 courses 数组应存储在学生对象内。

关于java - 是否可以通过此方法返回或以某种方式访问​​ "courses"中的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10987042/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com