• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Trouble printing out the indecies of a array.

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,

New to the forum and need some help if someone would be so kind.

Here is the problem, I am using two classes. One of the classes is StudentsInfo and the other is CourInfo. I have two arrays and I have one array as an argument in a constructor for another array. Now I am trying to a println in order to print out the content of one of the arrays(the array being used as a argument), using my main array as the calling object. I get a Array out of bounds error whenever I run the program. I have cut and paste the program below and put the lines of code in bold which are directly related to the issue. I have also given a brief description of certain variable to give further explanation. I have been at this for two weeks and this part is really stomping me, if anyone could help I would apprecaite it.


here is the println code:

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();
{



below is my class CourInfo:

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;
}
}
}
}



from the above

NS = number of students, each student has a set amount of information in his record

NC = number of courses taken per student. each student record has a integer in it signifying this number.

NS or number of students is what i used to loop through each student record.

I wanted to use NC or Number of Courses in order to loop through courses like i did before however when I try it says nc was not initialized. I am assuming thats because it was initialized inside of the for loop above so it is nested and the program is unable to read its value.

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;
}


}

 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have been at this for two weeks and this part is really stomping me, if anyone could help I would apprecaite it.



Hate to point out the obvious, but exceptions have traces that give you the class and line number where it occurred. It would help, if you give the exception trace too.

Henry
 
Ola Must
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry,

Sorry about that

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at StudentInfo.getCourses(StudentInfo.java:123)
at CourInfo.main(CourInfo.java:140)
Java Result: 1

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go to line 123 of StudentInfo.java. You should be in the getCourses() method. There should be an array there with a size of 3 -- the exception occurred while trying to access the 4th value.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, the error is here... I indented the code, to better reflect what the compiler sees...



It looks like you may have messed up the scope of the inner for loop. This causes that one line to run many times, but more importantly, it causes the j variable to be too big when the getCourses() method is called later.

Henry
[ November 27, 2005: Message edited by: Henry Wong ]
 
Ola Must
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry,

I love you!
 
reply
    Bookmark Topic Watch Topic
  • New Topic