![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 | |||
|
Newbie
Join Date: Nov 2005
Posts: 5
Rep Power: 0
![]() |
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:
Quote:
Below is the second class StudentInfo Quote:
|
|||
|
|
|
|
|
#2 |
|
Professional Programmer
|
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;
}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 ! |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Nov 2005
Posts: 5
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#4 |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Nov 2005
Posts: 5
Rep Power: 0
![]() |
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? |
|
|
|
|
|
#6 |
|
Professional Programmer
|
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 ! |
|
|
|
|
|
#7 | |||
|
Newbie
Join Date: Nov 2005
Posts: 5
Rep Power: 0
![]() |
Quote:
Quote:
Quote:
![]() |
|||
|
|
|
|
|
#8 |
|
Programmer
Join Date: Nov 2004
Posts: 84
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#9 |
|
Professional Programmer
|
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 ! |
|
|
|
|
|
#10 | ||||
|
Newbie
Join Date: Nov 2005
Posts: 5
Rep Power: 0
![]() |
In this part
Quote:
Then Quote:
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:
Quote:
But I received the same out of bounds. I will try what you have suggested groovicus to see if that sheds some light |
||||
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|