I wrote a function to read through all of the students in a students.txt file and add it to an arraylist
of that type. It works smooth but it's not adding the last student in the file to the arraylist. can u
help me fix my code so that 10 out of 10 students are added to an arraylist of type student?
Here is my code
public static ArrayList<Student> readDataFromFile(String fName)
throws FileNotFoundException{
ArrayList<Student> result = new ArrayList<>();
Scanner fileRdr = new Scanner(new File(fName));
String name = fileRdr.nextLine();
int sId = Integer.parseInt(fileRdr.nextLine());
int birthYr = Integer.parseInt(fileRdr.nextLine());
boolean sciMajor = Boolean.parseBoolean(fileRdr.nextLine());
ArrayList<Course> crseAL = new ArrayList<>();
String lineRead = fileRdr.nextLine();
while(fileRdr.hasNextLine()){
String [] data = lineRead.split(",");
Course c = new Course(data[0],data[1],Integer.parseInt(data[2]),data[3],data[4]);
crseAL.add(c);
lineRead = fileRdr.nextLine();
//You've read all courses for a particular student
if(lineRead.equals("")){//line empty
Student s = new Student(name,sId,birthYr,sciMajor);
s.setSchedule(crseAL);
result.add(s);
//read student info for next student
name = fileRdr.nextLine();
sId = Integer.parseInt(fileRdr.nextLine());
birthYr = Integer.parseInt(fileRdr.nextLine());
sciMajor = Boolean.parseBoolean(fileRdr.nextLine());
crseAL = new ArrayList<>();
lineRead = fileRdr.nextLine();
}
Here is the student.txt
Michael West
900753
2003
TruE
Art Appreciation,1090,3,sync,T
Data Structures,2735,5,online,MWF
Calculus,1070,4,in-person,MTWF
Personal Finance,1070,3,online,TBD
Health Ethics,2080,3,async,TR
Festus Park
900846
2000
False
Art Appreciation,1090,3,sync,T
Intro to CS,1724,4,in-person,MWTF
Statistics,2010,3,Online,MWF
Personal Finance,1070,3,online,TBD
Africa and the World,2090,3,sync,WF
Ahmed Nord
900963
2004
true
Intro to Philosophy,1070,3,online,TR
Data Structures,2735,5,online,MWF
Intro to Tennis,1000,3,in-person,MWF
Personal Finance,1070,3,online,TBD
Health Ethics,2080,3,async,TR
Tasha Ohlder
900102
1994
true
Intro to Sociology,1010,3,async,MW
Data Structures,2735,5,online,MWF
Statistics,2020,4,online,MWRF
Personal Finance,1070,3,online,TBD
Intro to Theology,1080,3,async,WF
Earl Mint
900159
2003
false
Calculus,1070,4,online,MTWF
Intro to CS,1724,4,in-person,MWTF
Personal Finance,1070,3,online,TBD
Health Ethics,2080,3,async,TR
English Composition,2010,3,online,MWF
Katie Easton
900951
2000
false
Discrete Structs,2550,3,online,TR
Intro to CS,1724,4,in-person,MWTF
Intro to Karate,1040,3,in-person,MWF
Personal Finance,1070,3,online,TBD
Health Ethics,2080,3,async,TR
Kennedy Dotson
900237
1999
True
Intro to Philosophy,1070,3,online,TR
Data Structures,2735,5,online,MWF
Intro to Data Science,1070,4,in-person,MWF
Personal Finance,1070,3,online,TBD
Nick Peoples
900856
2001
False
Intro to Sociology,1010,3,online,MW
Intro to CS,1724,4,in-person,MWTF
Intro to Tennis,1000,3,in-person,MWF
Personal Finance,1070,3,online,TBD
Olivia Priest
900123
1999
True
Intro to Philosophy,1070,3,online,TR
Data Structures,2735,5,online,MWF
Intro to Data Science,1070,4,in-person,MWF
Macro Economics,2020,3,sync,TW
Personal Finance,1070,3,online,TBD
Rashad Black
900456
2001
False
Intro to Sociology,1010,3,online,MW
Intro to CS,1724,4,in-person,MWTF
Intro to Tennis,1000,3,in-person,MWF
Personal Finance,1070,3,online,TBD
How Things Work,1010,3,async,WF
Here is my code
public static ArrayList<Student> readDataFromFile(String fName)
throws FileNotFoundException{
ArrayList<Student> result = new ArrayList<>();
Scanner fileRdr = new Scanner(new File(fName));
String name = fileRdr.nextLine();
int sId = Integer.parseInt(fileRdr.nextLine());
int birthYr = Integer.parseInt(fileRdr.nextLine());
boolean sciMajor = Boolean.parseBoolean(fileRdr.nextLine());
ArrayList<Course> crseAL = new ArrayList<>();
String lineRead = fileRdr.nextLine();
while(fileRdr.hasNextLine()){
String [] data = lineRead.split(",");
Course c = new Course(data[0],data[1],Integer.parseInt(data[2]),data[3],data[4]);
crseAL.add(c);
lineRead = fileRdr.nextLine();
//You've read all courses for a particular student
if(lineRead.equals("")){//line empty
Student s = new Student(name,sId,birthYr,sciMajor);
s.setSchedule(crseAL);
result.add(s);
//read student info for next student
name = fileRdr.nextLine();
sId = Integer.parseInt(fileRdr.nextLine());
birthYr = Integer.parseInt(fileRdr.nextLine());
sciMajor = Boolean.parseBoolean(fileRdr.nextLine());
crseAL = new ArrayList<>();
lineRead = fileRdr.nextLine();
}
public static ArrayList<Student> readDataFromFile(String fName)
throws FileNotFoundException{
ArrayList<Student> result = new ArrayList<>();
Scanner fileRdr = new Scanner(new File(fName));
String name = fileRdr.nextLine();
int sId = Integer.parseInt(fileRdr.nextLine());
int birthYr = Integer.parseInt(fileRdr.nextLine());
boolean sciMajor = Boolean.parseBoolean(fileRdr.nextLine());
ArrayList<Course> crseAL = new ArrayList<>();
String lineRead = fileRdr.nextLine();
while(fileRdr.hasNextLine()){
String [] data = lineRead.split(",");
Course c = new Course(data[0],data[1],Integer.parseInt(data[2]),data[3],data[4]);
crseAL.add(c);
lineRead = fileRdr.nextLine();
//You've read all courses for a particular student
if(lineRead.equals("")){//line empty
Student s = new Student(name,sId,birthYr,sciMajor);
s.setSchedule(crseAL);
result.add(s);
//read student info for next student
name = fileRdr.nextLine();
sId = Integer.parseInt(fileRdr.nextLine());
birthYr = Integer.parseInt(fileRdr.nextLine());
sciMajor = Boolean.parseBoolean(fileRdr.nextLine());
crseAL = new ArrayList<>();
lineRead = fileRdr.nextLine();
}
Here is the student.txt
Michael West
900753
2003
TruE
Art Appreciation,1090,3,sync,T
Data Structures,2735,5,online,MWF
Calculus,1070,4,in-person,MTWF
Personal Finance,1070,3,online,TBD
Health Ethics,2080,3,async,TR
Festus Park
900846
2000
False
Art Appreciation,1090,3,sync,T
Intro to CS,1724,4,in-person,MWTF
Statistics,2010,3,Online,MWF
Personal Finance,1070,3,online,TBD
Africa and the World,2090,3,sync,WF
Ahmed Nord
900963
2004
true
Intro to Philosophy,1070,3,online,TR
Data Structures,2735,5,online,MWF
Intro to Tennis,1000,3,in-person,MWF
Personal Finance,1070,3,online,TBD
Health Ethics,2080,3,async,TR
Tasha Ohlder
900102
1994
true
Intro to Sociology,1010,3,async,MW
Data Structures,2735,5,online,MWF
Statistics,2020,4,online,MWRF
Personal Finance,1070,3,online,TBD
Intro to Theology,1080,3,async,WF
Earl Mint
900159
2003
false
Calculus,1070,4,online,MTWF
Intro to CS,1724,4,in-person,MWTF
Personal Finance,1070,3,online,TBD
Health Ethics,2080,3,async,TR
English Composition,2010,3,online,MWF
Katie Easton
900951
2000
false
Discrete Structs,2550,3,online,TR
Intro to CS,1724,4,in-person,MWTF
Intro to Karate,1040,3,in-person,MWF
Personal Finance,1070,3,online,TBD
Health Ethics,2080,3,async,TR
Kennedy Dotson
900237
1999
True
Intro to Philosophy,1070,3,online,TR
Data Structures,2735,5,online,MWF
Intro to Data Science,1070,4,in-person,MWF
Personal Finance,1070,3,online,TBD
Nick Peoples
900856
2001
False
Intro to Sociology,1010,3,online,MW
Intro to CS,1724,4,in-person,MWTF
Intro to Tennis,1000,3,in-person,MWF
Personal Finance,1070,3,online,TBD
Olivia Priest
900123
1999
True
Intro to Philosophy,1070,3,online,TR
Data Structures,2735,5,online,MWF
Intro to Data Science,1070,4,in-person,MWF
Macro Economics,2020,3,sync,TW
Personal Finance,1070,3,online,TBD
Rashad Black
900456
2001
False
Intro to Sociology,1010,3,online,MW
Intro to CS,1724,4,in-person,MWTF
Intro to Tennis,1000,3,in-person,MWF
Personal Finance,1070,3,online,TBD
How Things Work,1010,3,async,WF
Michael West
900753
2003
TruE
Art Appreciation,1090,3,sync,T
Data Structures,2735,5,online,MWF
Calculus,1070,4,in-person,MTWF
Personal Finance,1070,3,online,TBD
Health Ethics,2080,3,async,TR
Festus Park
900846
2000
False
Art Appreciation,1090,3,sync,T
Intro to CS,1724,4,in-person,MWTF
Statistics,2010,3,Online,MWF
Personal Finance,1070,3,online,TBD
Africa and the World,2090,3,sync,WF
Ahmed Nord
900963
2004
true
Intro to Philosophy,1070,3,online,TR
Data Structures,2735,5,online,MWF
Intro to Tennis,1000,3,in-person,MWF
Personal Finance,1070,3,online,TBD
Health Ethics,2080,3,async,TR
Tasha Ohlder
900102
1994
true
Intro to Sociology,1010,3,async,MW
Data Structures,2735,5,online,MWF
Statistics,2020,4,online,MWRF
Personal Finance,1070,3,online,TBD
Intro to Theology,1080,3,async,WF
Earl Mint
900159
2003
false
Calculus,1070,4,online,MTWF
Intro to CS,1724,4,in-person,MWTF
Personal Finance,1070,3,online,TBD
Health Ethics,2080,3,async,TR
English Composition,2010,3,online,MWF
Katie Easton
900951
2000
false
Discrete Structs,2550,3,online,TR
Intro to CS,1724,4,in-person,MWTF
Intro to Karate,1040,3,in-person,MWF
Personal Finance,1070,3,online,TBD
Health Ethics,2080,3,async,TR
Kennedy Dotson
900237
1999
True
Intro to Philosophy,1070,3,online,TR
Data Structures,2735,5,online,MWF
Intro to Data Science,1070,4,in-person,MWF
Personal Finance,1070,3,online,TBD
Nick Peoples
900856
2001
False
Intro to Sociology,1010,3,online,MW
Intro to CS,1724,4,in-person,MWTF
Intro to Tennis,1000,3,in-person,MWF
Personal Finance,1070,3,online,TBD
Olivia Priest
900123
1999
True
Intro to Philosophy,1070,3,online,TR
Data Structures,2735,5,online,MWF
Intro to Data Science,1070,4,in-person,MWF
Macro Economics,2020,3,sync,TW
Personal Finance,1070,3,online,TBD
Rashad Black
900456
2001
False
Intro to Sociology,1010,3,online,MW
Intro to CS,1724,4,in-person,MWTF
Intro to Tennis,1000,3,in-person,MWF
Personal Finance,1070,3,online,TBD
How Things Work,1010,3,async,WF
I wrote a function to read through all of the students in a .pdf

I wrote a function to read through all of the students in a .pdf

  • 1.
    I wrote afunction to read through all of the students in a students.txt file and add it to an arraylist of that type. It works smooth but it's not adding the last student in the file to the arraylist. can u help me fix my code so that 10 out of 10 students are added to an arraylist of type student? Here is my code public static ArrayList<Student> readDataFromFile(String fName) throws FileNotFoundException{ ArrayList<Student> result = new ArrayList<>(); Scanner fileRdr = new Scanner(new File(fName)); String name = fileRdr.nextLine(); int sId = Integer.parseInt(fileRdr.nextLine()); int birthYr = Integer.parseInt(fileRdr.nextLine()); boolean sciMajor = Boolean.parseBoolean(fileRdr.nextLine()); ArrayList<Course> crseAL = new ArrayList<>(); String lineRead = fileRdr.nextLine(); while(fileRdr.hasNextLine()){ String [] data = lineRead.split(","); Course c = new Course(data[0],data[1],Integer.parseInt(data[2]),data[3],data[4]); crseAL.add(c); lineRead = fileRdr.nextLine(); //You've read all courses for a particular student if(lineRead.equals("")){//line empty Student s = new Student(name,sId,birthYr,sciMajor); s.setSchedule(crseAL); result.add(s); //read student info for next student name = fileRdr.nextLine(); sId = Integer.parseInt(fileRdr.nextLine()); birthYr = Integer.parseInt(fileRdr.nextLine()); sciMajor = Boolean.parseBoolean(fileRdr.nextLine()); crseAL = new ArrayList<>(); lineRead = fileRdr.nextLine(); } Here is the student.txt Michael West 900753 2003
  • 2.
    TruE Art Appreciation,1090,3,sync,T Data Structures,2735,5,online,MWF Calculus,1070,4,in-person,MTWF PersonalFinance,1070,3,online,TBD Health Ethics,2080,3,async,TR Festus Park 900846 2000 False Art Appreciation,1090,3,sync,T Intro to CS,1724,4,in-person,MWTF Statistics,2010,3,Online,MWF Personal Finance,1070,3,online,TBD Africa and the World,2090,3,sync,WF Ahmed Nord 900963 2004 true Intro to Philosophy,1070,3,online,TR Data Structures,2735,5,online,MWF Intro to Tennis,1000,3,in-person,MWF Personal Finance,1070,3,online,TBD Health Ethics,2080,3,async,TR Tasha Ohlder 900102 1994 true Intro to Sociology,1010,3,async,MW Data Structures,2735,5,online,MWF Statistics,2020,4,online,MWRF Personal Finance,1070,3,online,TBD Intro to Theology,1080,3,async,WF Earl Mint 900159 2003 false Calculus,1070,4,online,MTWF Intro to CS,1724,4,in-person,MWTF
  • 3.
    Personal Finance,1070,3,online,TBD Health Ethics,2080,3,async,TR EnglishComposition,2010,3,online,MWF Katie Easton 900951 2000 false Discrete Structs,2550,3,online,TR Intro to CS,1724,4,in-person,MWTF Intro to Karate,1040,3,in-person,MWF Personal Finance,1070,3,online,TBD Health Ethics,2080,3,async,TR Kennedy Dotson 900237 1999 True Intro to Philosophy,1070,3,online,TR Data Structures,2735,5,online,MWF Intro to Data Science,1070,4,in-person,MWF Personal Finance,1070,3,online,TBD Nick Peoples 900856 2001 False Intro to Sociology,1010,3,online,MW Intro to CS,1724,4,in-person,MWTF Intro to Tennis,1000,3,in-person,MWF Personal Finance,1070,3,online,TBD Olivia Priest 900123 1999 True Intro to Philosophy,1070,3,online,TR Data Structures,2735,5,online,MWF Intro to Data Science,1070,4,in-person,MWF Macro Economics,2020,3,sync,TW Personal Finance,1070,3,online,TBD Rashad Black
  • 4.
    900456 2001 False Intro to Sociology,1010,3,online,MW Introto CS,1724,4,in-person,MWTF Intro to Tennis,1000,3,in-person,MWF Personal Finance,1070,3,online,TBD How Things Work,1010,3,async,WF Here is my code public static ArrayList<Student> readDataFromFile(String fName) throws FileNotFoundException{ ArrayList<Student> result = new ArrayList<>(); Scanner fileRdr = new Scanner(new File(fName)); String name = fileRdr.nextLine(); int sId = Integer.parseInt(fileRdr.nextLine()); int birthYr = Integer.parseInt(fileRdr.nextLine()); boolean sciMajor = Boolean.parseBoolean(fileRdr.nextLine()); ArrayList<Course> crseAL = new ArrayList<>(); String lineRead = fileRdr.nextLine(); while(fileRdr.hasNextLine()){ String [] data = lineRead.split(","); Course c = new Course(data[0],data[1],Integer.parseInt(data[2]),data[3],data[4]); crseAL.add(c); lineRead = fileRdr.nextLine(); //You've read all courses for a particular student if(lineRead.equals("")){//line empty Student s = new Student(name,sId,birthYr,sciMajor); s.setSchedule(crseAL); result.add(s); //read student info for next student name = fileRdr.nextLine(); sId = Integer.parseInt(fileRdr.nextLine()); birthYr = Integer.parseInt(fileRdr.nextLine()); sciMajor = Boolean.parseBoolean(fileRdr.nextLine()); crseAL = new ArrayList<>(); lineRead = fileRdr.nextLine(); }
  • 5.
    public static ArrayList<Student>readDataFromFile(String fName) throws FileNotFoundException{ ArrayList<Student> result = new ArrayList<>(); Scanner fileRdr = new Scanner(new File(fName)); String name = fileRdr.nextLine(); int sId = Integer.parseInt(fileRdr.nextLine()); int birthYr = Integer.parseInt(fileRdr.nextLine()); boolean sciMajor = Boolean.parseBoolean(fileRdr.nextLine()); ArrayList<Course> crseAL = new ArrayList<>(); String lineRead = fileRdr.nextLine(); while(fileRdr.hasNextLine()){ String [] data = lineRead.split(","); Course c = new Course(data[0],data[1],Integer.parseInt(data[2]),data[3],data[4]); crseAL.add(c); lineRead = fileRdr.nextLine(); //You've read all courses for a particular student if(lineRead.equals("")){//line empty Student s = new Student(name,sId,birthYr,sciMajor); s.setSchedule(crseAL); result.add(s); //read student info for next student
  • 6.
    name = fileRdr.nextLine(); sId= Integer.parseInt(fileRdr.nextLine()); birthYr = Integer.parseInt(fileRdr.nextLine()); sciMajor = Boolean.parseBoolean(fileRdr.nextLine()); crseAL = new ArrayList<>(); lineRead = fileRdr.nextLine(); } Here is the student.txt Michael West 900753 2003 TruE Art Appreciation,1090,3,sync,T Data Structures,2735,5,online,MWF Calculus,1070,4,in-person,MTWF Personal Finance,1070,3,online,TBD Health Ethics,2080,3,async,TR Festus Park 900846 2000 False Art Appreciation,1090,3,sync,T Intro to CS,1724,4,in-person,MWTF Statistics,2010,3,Online,MWF Personal Finance,1070,3,online,TBD Africa and the World,2090,3,sync,WF Ahmed Nord 900963 2004 true
  • 7.
    Intro to Philosophy,1070,3,online,TR DataStructures,2735,5,online,MWF Intro to Tennis,1000,3,in-person,MWF Personal Finance,1070,3,online,TBD Health Ethics,2080,3,async,TR Tasha Ohlder 900102 1994 true Intro to Sociology,1010,3,async,MW Data Structures,2735,5,online,MWF Statistics,2020,4,online,MWRF Personal Finance,1070,3,online,TBD Intro to Theology,1080,3,async,WF Earl Mint 900159 2003 false Calculus,1070,4,online,MTWF Intro to CS,1724,4,in-person,MWTF Personal Finance,1070,3,online,TBD Health Ethics,2080,3,async,TR English Composition,2010,3,online,MWF Katie Easton 900951 2000 false Discrete Structs,2550,3,online,TR Intro to CS,1724,4,in-person,MWTF Intro to Karate,1040,3,in-person,MWF Personal Finance,1070,3,online,TBD Health Ethics,2080,3,async,TR Kennedy Dotson 900237 1999 True Intro to Philosophy,1070,3,online,TR Data Structures,2735,5,online,MWF Intro to Data Science,1070,4,in-person,MWF
  • 8.
    Personal Finance,1070,3,online,TBD Nick Peoples 900856 2001 False Introto Sociology,1010,3,online,MW Intro to CS,1724,4,in-person,MWTF Intro to Tennis,1000,3,in-person,MWF Personal Finance,1070,3,online,TBD Olivia Priest 900123 1999 True Intro to Philosophy,1070,3,online,TR Data Structures,2735,5,online,MWF Intro to Data Science,1070,4,in-person,MWF Macro Economics,2020,3,sync,TW Personal Finance,1070,3,online,TBD Rashad Black 900456 2001 False Intro to Sociology,1010,3,online,MW Intro to CS,1724,4,in-person,MWTF Intro to Tennis,1000,3,in-person,MWF Personal Finance,1070,3,online,TBD How Things Work,1010,3,async,WF Michael West 900753 2003 TruE Art Appreciation,1090,3,sync,T Data Structures,2735,5,online,MWF
  • 9.
    Calculus,1070,4,in-person,MTWF Personal Finance,1070,3,online,TBD Health Ethics,2080,3,async,TR FestusPark 900846 2000 False Art Appreciation,1090,3,sync,T Intro to CS,1724,4,in-person,MWTF Statistics,2010,3,Online,MWF Personal Finance,1070,3,online,TBD Africa and the World,2090,3,sync,WF Ahmed Nord 900963 2004 true Intro to Philosophy,1070,3,online,TR Data Structures,2735,5,online,MWF Intro to Tennis,1000,3,in-person,MWF Personal Finance,1070,3,online,TBD Health Ethics,2080,3,async,TR
  • 10.
    Tasha Ohlder 900102 1994 true Intro toSociology,1010,3,async,MW Data Structures,2735,5,online,MWF Statistics,2020,4,online,MWRF Personal Finance,1070,3,online,TBD Intro to Theology,1080,3,async,WF Earl Mint 900159 2003 false Calculus,1070,4,online,MTWF Intro to CS,1724,4,in-person,MWTF Personal Finance,1070,3,online,TBD Health Ethics,2080,3,async,TR English Composition,2010,3,online,MWF Katie Easton 900951
  • 11.
    2000 false Discrete Structs,2550,3,online,TR Intro toCS,1724,4,in-person,MWTF Intro to Karate,1040,3,in-person,MWF Personal Finance,1070,3,online,TBD Health Ethics,2080,3,async,TR Kennedy Dotson 900237 1999 True Intro to Philosophy,1070,3,online,TR Data Structures,2735,5,online,MWF Intro to Data Science,1070,4,in-person,MWF Personal Finance,1070,3,online,TBD Nick Peoples 900856 2001 False Intro to Sociology,1010,3,online,MW
  • 12.
    Intro to CS,1724,4,in-person,MWTF Introto Tennis,1000,3,in-person,MWF Personal Finance,1070,3,online,TBD Olivia Priest 900123 1999 True Intro to Philosophy,1070,3,online,TR Data Structures,2735,5,online,MWF Intro to Data Science,1070,4,in-person,MWF Macro Economics,2020,3,sync,TW Personal Finance,1070,3,online,TBD Rashad Black 900456 2001 False Intro to Sociology,1010,3,online,MW Intro to CS,1724,4,in-person,MWTF Intro to Tennis,1000,3,in-person,MWF Personal Finance,1070,3,online,TBD How Things Work,1010,3,async,WF