op = new PrintWriter(new FileOutputStream(opFileName));
for (i = 0; i < ns; i++){
for (j = 0; j < students[i].getNumCourses(); j++)
op.println(students[i].getFirst());
op.println(students[i].getLast());
op.println(students[i].getEmail());
op.println(students[i].getYearOfGraduation());
op.println(students[i].getMajor());
op.println(students[i].getGradePointAverage());
op.println(students[i].getNumCourses());
op.println(students[i].getCourses(j).getDcn());
op.println(students[i].getCourses(j).getDcm());
op.println(students[i].getCourses(j).getTcm());
op.println(students[i].getDaysWeekAvail());
}
op.close();
{
import java.io.*;
import java.util.*;
/**
*
*
*/
public class CourInfo {
private String tcm; // times that the course meets ea. week
private String dcn; //Department and course number
private String dcm; // The days class his held ea. week.
private int nc = 0; // number of courses
/** Creates a new instance of CourInfo */
public CourInfo() {
}
public CourInfo(String dn, String dm, String tm){
dcn = dn;
dcm = dm;
tcm = tm;
}
public void setDcn(String dn){
dcn = dn;
}
public void setDcm(String dm){
dcm = dm;
}
public void setTcm(String tm){
tcm = tm;
}
public String getDcn(){
return dcn;
}
public String getDcm(){
return dcm;
}
public String getTcm(){
return tcm;
}
public static void main(String[] args) {
BufferedReader is; // is = input stream
PrintWriter op; // op = output stream
String first;
String last;
String email;
String maj; // Students Major
String dows; // days of the week student is available
String dm; // The days class his held ea. week.
String dn; //Department and course number
String tm;
int ns; // # of students records to be used in increment
int nc; // # of course records to be used in increment
double gpa; // grade point average
int cc; // count for course increment
int sc; // count for student increment
int i;
int j;
int yog; // year of graduation
/*asks for user to enter the location for the file which
*the user wants to be read from.
*/
System.out.println("Enter location of the file:");
Scanner keyboard = new Scanner(System.in);
String ipFileName = keyboard.next();
try {
is = new BufferedReader(new FileReader((ipFileName)));
ns = Integer.parseInt(is.readLine());
StudentInfo [] students = new StudentInfo[ns];
for (i = 0; i < ns; i++){
last = is.readLine();
first = is.readLine();
email = is.readLine();
yog = Integer.parseInt(is.readLine());
maj = is.readLine();
gpa = Double.parseDouble(is.readLine());
nc = Integer.parseInt(is.readLine());
CourInfo[] courses = new CourInfo[nc];
for (j = 0; j < nc; j++){
dn = is.readLine(); //Department and course number
dm = is.readLine(); // The days class his held ea. week.
tm = is.readLine(); // times that the course meets ea. week
courses[j] = new CourInfo(dn, dm, tm);
}
dows = is.readLine();
students[i] = new StudentInfo(first, last, email, yog, maj, gpa, nc, courses, dows);
}
is.close();
bubbSort(students);
System.out.println("Enter location of file you wish to output to:");
String opFileName = keyboard.next();
op = new PrintWriter(new FileOutputStream(opFileName));
for (i = 0; i < ns; i++){
for (j = 0; j < students[i].getNumCourses(); j++)
op.println(students[i].getFirst());
op.println(students[i].getLast());
op.println(students[i].getEmail());
op.println(students[i].getYearOfGraduation());
op.println(students[i].getMajor());
op.println(students[i].getGradePointAverage());
op.println(students[i].getNumCourses());
op.println(students[i].getCourses(j).getDcn());
op.println(students[i].getCourses(j).getDcm());
op.println(students[i].getCourses(j).getTcm());
op.println(students[i].getDaysWeekAvail());
}
op.close();
}
catch (FileNotFoundException e) {
System.out.println("File not found");
System.exit(0);
}
catch(IOException e) {
System.out.println("Error reading from or writing to the file");
}
}
public static void bubbSort(StudentInfo[] s){
int index, indexOfSmallest, pass, i;
StudentInfo temp;
for (pass = 0; pass < s.length - 1; pass++)
{
for (index = 0; index < s.length - 1 - pass; index++)
if (s[index + 1].getGradePointAverage() > s[index].getGradePointAverage())
{
temp = s[index];
s[index] = s[index + 1];
s[index + 1] = temp;
}
}
}
}
public class StudentInfo {
private String firstName;
private String lastName;
private String emailAdd;
private int yearOfGraduation;
private String major;
private double gradePointAverage;
private String daysWeekAvail;
private CourInfo[] courses;
private int nc; //number of courses
/** Creates a new instance of StudentInfo */
public StudentInfo() {
}
public StudentInfo(String first, String last, String email, int yog, String maj, double gpa, int n, CourInfo[] course, String dwa){
firstName = first;
lastName = last;
emailAdd = email;
yearOfGraduation = yog;
major = maj;
gradePointAverage = gpa;
nc = n;
courses = course;
daysWeekAvail = dwa;
}
public void setFirst(String first){
firstName = first;
}
public void setLast(String last){
lastName = last;
}
public void setEmail(String email){
emailAdd = email;
}
public void setGradePointAverage(int gpa){
gradePointAverage = gpa;
}
public void setNumCourses(int numCour){
nc = numCour;
}
public void setCourses(CourInfo [] course){
courses = course;
}
public void setDaysWeekAvail(String dwa){
daysWeekAvail = dwa;
}
public void setMajor(String maj){
major = maj;
}
public void setYearOfGraduation(int yog){
yearOfGraduation = yog;
}
public String getFirst(){
return firstName;
}
public String getLast(){
return lastName;
}
public String getEmail(){
return emailAdd;
}
public int getYearOfGraduation(){
return yearOfGraduation;
}
public int getNumCourses(){
return nc;
}
public String getMajor(){
return major;
}
public double getGradePointAverage(){
return gradePointAverage;
}
public CourInfo getCourses(int j){
return courses[j];
public String getDaysWeekAvail(){
return daysWeekAvail;
}
}
I have been at this for two weeks and this part is really stomping me, if anyone could help I would apprecaite it.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at StudentInfo.getCourses(StudentInfo.java:123)
at CourInfo.main(CourInfo.java:140)
Java Result: 1