Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 25th, 2005, 1:01 PM   #1
Alo Tsum
Newbie
 
Join Date: Nov 2005
Posts: 5
Rep Power: 0 Alo Tsum is on a distinguished road
Array issues :(

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, using my main array as the calling object. It prints but it will only print the memory address and not the actual data. Please anyone who can help me I am desperate. I have been at this for two weeks .


here is the code for the println:

Quote:
op = new PrintWriter(new FileOutputStream(opFileName));
for (i = 0; i < ns; i++){

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());
op.println(students[i].getDaysWeekAvail());
}
op.close();
Below is the CourInfo class:

Quote:
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++){

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

Below is the second class StudentInfo

Quote:
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(){

return courses;
}

public String getDaysWeekAvail(){

return daysWeekAvail;
}


}
Alo Tsum is offline   Reply With Quote
Old Nov 25th, 2005, 2:18 PM   #2
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
public CourInfo [] getCourses(){
  return courses;
}

This returns what ? an object : courses. In that object you have :
 public CourInfo(String dn, String dm, String tm){
  dcn = dn;
  dcm = dm;
  tcm = tm;
}
So when you do : op.println(students[i].getCourses());
It doesn't know what to print. maybe you should do something like :

op.println(students[i].getCourses(i).getDcn());

and
public CourInfo getCourses(int i){
  return courses[i];
}
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Nov 25th, 2005, 7:31 PM   #3
Alo Tsum
Newbie
 
Join Date: Nov 2005
Posts: 5
Rep Power: 0 Alo Tsum is on a distinguished road
I appreciate the help big time!!!!!

I tried to run it but the following occured.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3

error when I tried to compile it. Actually that occurs in both classes. I will keep working with the code you gave me to see if I can get it to work, and I will continue checking back here.

thank you.
Alo Tsum is offline   Reply With Quote
Old Nov 25th, 2005, 10:32 PM   #4
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 4 groovicus is on a distinguished road
The error that you are getting is common to, pardon the term, "rookie" programmers. Even experienced hands do it from time to time.. it just becomes easier to diagnose. Without giving away the answer, so you know what the error means?
__________________
HijackThis Team-SFDC
groovicus is offline   Reply With Quote
Old Nov 25th, 2005, 11:42 PM   #5
Alo Tsum
Newbie
 
Join Date: Nov 2005
Posts: 5
Rep Power: 0 Alo Tsum is on a distinguished road
groovicus,

I am a mega rookie to programming. I am taking my second programming course which is Java. I took C++ the previous semester but didn’t get much from it. 

As for the error, I assumed it to mean that I have more index variables than there are actual indices in the array. Is that correct?
Alo Tsum is offline   Reply With Quote
Old Nov 25th, 2005, 11:50 PM   #6
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Yea, that's what it means, you'r going outside the array.
The problem doesn't apear here. Maybe it's because of the test file (the one with the data) .. or maybe you mistyped something
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Nov 26th, 2005, 9:23 AM   #7
Alo Tsum
Newbie
 
Join Date: Nov 2005
Posts: 5
Rep Power: 0 Alo Tsum is on a distinguished road
Quote:
Originally Posted by xavier
Yea, that's what it means, you'r going outside the array.
The problem doesn't apear here. Maybe it's because of the test file (the one with the data) .. or maybe you mistyped something
Maybe it is the text file, im not sure. Here is the code i changed. I re typed everything and still get those errors.

Quote:
op = new PrintWriter(new FileOutputStream(opFileName));
for (i = 0; i < ns; i++){

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].getCourses(i).getDcn());
op.println(students[i].getCourses(i).getDcm());
op.println(students[i].getCourses(i).getTcm());
op.println(students[i].getDaysWeekAvail());
}
and in the StudInfo class

Quote:
public CourInfo getCourses(int i){

return courses[i];

}
I have also attached the source text file we were assigned to use. This is the toughest assignment he has given us so far. I apprecaite all the help. I don't know what I could be doing wrong
Attached Files
File Type: txt StudInp.txt (767 Bytes, 27 views)
Alo Tsum is offline   Reply With Quote
Old Nov 26th, 2005, 9:35 AM   #8
groovicus
Programmer
 
Join Date: Nov 2004
Posts: 84
Rep Power: 4 groovicus is on a distinguished road
Right before that loop, do a System.out.println so that you can see what the value of ns is supposed to be. Right after the loop starts, do a system.out.println to see what the value of 'i' is each time through the loop.

If you use code tags instead of quote tags for your code, it will be easier for all of us to read. Right now I am havinga hard tiime seeing which catches go which which try.

Do you know the exact line of code on which you are bombing?
__________________
HijackThis Team-SFDC
groovicus is offline   Reply With Quote
Old Nov 26th, 2005, 12:35 PM   #9
xavier
Professional Programmer
 
xavier's Avatar
 
Join Date: Oct 2004
Location: .ro
Posts: 383
Rep Power: 4 xavier is on a distinguished road
Send a message via Yahoo to xavier
Hmm .. the problem is that for example :
The 1st student has 3 courses
The 2nd student has 2 courses
........................................
The n student has 2 or 3 courses .. certainly not n courses , so that's where the problem apears.

You try to get op.println(students[i].getCourses(i).getDcn());
so getCourses(i) ... for the n student tries : getCourses(n) .. and that array has no n elements , but 2 or 3 .

So , try to find a way arround that
__________________
Don't take life too seriously, it's not permanent !
xavier is offline   Reply With Quote
Old Nov 26th, 2005, 12:41 PM   #10
Alo Tsum
Newbie
 
Join Date: Nov 2005
Posts: 5
Rep Power: 0 Alo Tsum is on a distinguished road
In this part

Quote:
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);
}

Then

Quote:
op = new PrintWriter(new FileOutputStream(opFileName));
for (i = 0; i < ns; i++){

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());
op.println(students[i].getDaysWeekAvail());
}
op.close();
}
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.

I also tried this in the CourInfo class

Quote:
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();
}
and this in the StudentInfo class

Quote:
public CourInfo getCourses(int j){

return courses[j];

}

But I received the same out of bounds.

I will try what you have suggested groovicus to see if that sheds some light
Alo Tsum is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:36 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC