java - Is it possible to return or somehow access the data from "courses" from this method? -
i'm working on first programming assignment java , had question. put course[] inside of student[] seem encountering nullpointerexception error , can't figure out why.
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 not found or opened."); system.exit(0); } int numberofstudents = inputstream.nextint(); int tuitionperhour = inputstream.nextint(); student[] students = new student[numberofstudents]; (int = 0; < 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]; (i = 0; < 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; }
the formatting input file is:
3 345 lisa miller 890238 y 2 mathematics mth345 4 physics phy357 3 b bill wilton 798324 n 2 english eng378 3 b philosophy phl534 3
where courses has information courses, , students has information students.
i add array (or better yet, arraylist or map) student
contains classes student taking.
with you're doing, how determining classes go students?
try adding following instance variable
private list<course> courses;
to student
, implement following methods add list or return whole thing.
void addcourse(course c) { /*code here*/ }; list getcourses() {/* code here */} ;
and reading files can real pain, have ok, @ least now. loop course
instantiated include call student.addcourse(course)
. you'll golden.
be advised high level overview there learnage in here you. post , we'll help.
Comments
Post a Comment