•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 455,974 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,807 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 645 | Replies: 7 | Solved
![]() |
•
•
Join Date: Nov 2007
Posts: 14
Reputation:
Rep Power: 2
Solved Threads: 0
If someone has a second, could you please take a look at this. This is just a small part to a large program I have to write for school. Right now I'm just trying to get the arrays to load.
I can't get my two dimesinal array to work (the loadAssignments method at the bottom). It compiles ok, but then I get an error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at finalGradeReport.loadAssignments(finalGradeReport.java:68)
at finalGradeReport.main(finalGradeReport.java:31)
I researched that error, and I guess it means an illegal index is being accessed..but I'm not sure why.
The assignments files looks like this...
01 90 80 70 95 96 97 80 80 70 100
02 71 72 73 74 75 76 77 78 89 80
03 81 82 83 84 85 86 87 88 89 90
04 91 92 81 82 83 91 71 0 0 0
05 100 100 100 100 100 99 99 99 88 77
student numbers being in the first column.
I'm guessing it's something simple, but I really don't want to wait til my next class to figure it out.
I can't get my two dimesinal array to work (the loadAssignments method at the bottom). It compiles ok, but then I get an error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at finalGradeReport.loadAssignments(finalGradeReport.java:68)
at finalGradeReport.main(finalGradeReport.java:31)
I researched that error, and I guess it means an illegal index is being accessed..but I'm not sure why.
The assignments files looks like this...
01 90 80 70 95 96 97 80 80 70 100
02 71 72 73 74 75 76 77 78 89 80
03 81 82 83 84 85 86 87 88 89 90
04 91 92 81 82 83 91 71 0 0 0
05 100 100 100 100 100 99 99 99 88 77
student numbers being in the first column.
I'm guessing it's something simple, but I really don't want to wait til my next class to figure it out.
public static void main(String[] args) //Main Program
{ //Start Main
String name="";
int totalStudents;
Keyboard kbd; //create keyboard
kbd = new Keyboard ();
InputFile students; //create file to read
students = new InputFile ("H:\\MaxSync\\School\\SLCC\\CIS 1030\\finalGradeReport\\students.txt");
InputFile assignments; //create file to read
assignments = new InputFile ("H:\\MaxSync\\School\\SLCC\\CIS 1030\\finalGradeReport\\assignments.txt");
OutputFile report; //creat new outputfile
report = new OutputFile ("H:\\MaxSync\\School\\SLCC\\CIS 1030\\finalGradeReport\\report.txt");
//header (report);
int [] numberArray;
numberArray = new int [5];
String [] nameArray;
nameArray = new String [5];
loadStudentInfo (students, numberArray, nameArray);
loadAssignments (assignments, numberArray);
}
public static void loadStudentInfo (InputFile students, int [] numberArray, String [] nameArray)
{
int x=0;
for (x = 0; !students.eof(); x++)
{
numberArray[x]=students.readInt ();
nameArray[x]=students.readLine ();
}
}
public static void loadAssignments (InputFile assignments, int [] numberArray)
{
int x;
int y;
int [] [] matrix;
matrix = new int [5] [10];
for (x = 0, y = 0; !assignments.eof(); x++)
{
numberArray[x]=assignments.readInt ();
matrix[x][y]=assignments.readInt ();
}
}
}
•
•
Join Date: Nov 2007
Location: Belgium
Posts: 62
Reputation:
Rep Power: 1
Solved Threads: 6
By giving your arrays to that method, you give them a local copy of the arrays.
This means that what ever you do inside that method, happens to the copy. In orde to get the result back into your initial arrays, you need to let your methods return the adjustments you made.
Let me know if it works.
Dominic
This means that what ever you do inside that method, happens to the copy. In orde to get the result back into your initial arrays, you need to let your methods return the adjustments you made.
Let me know if it works.
public class Main {
public static void main(String[] args) {
int x = 0; //Creating a local variable.
Main m = new Main();
m.addOne(x); //handing over a copy to that method.
System.out.println("" + x); //Will print out '0' even after addOne method.
}
public void addOne(int x) {
x++; //The new value of x will only be known within the scope of this addOne method.
}
}Dominic
Last edited by Black Box : Nov 28th, 2007 at 5:04 pm. Reason: Added a little code to make clear what I meant
•
•
Join Date: Nov 2007
Posts: 14
Reputation:
Rep Power: 2
Solved Threads: 0
hmm...but from what I learned I can only return one item back from a method.. So I'm guessing my whole method would be a no go since there is more then one array. (or am I just clueless)
but it's strange, my first method works, the "loadStudentInfo" method.
I created the "matrix" array in the second method...I wonder if it would help if I created it in the main like I did with the first. I'll have to try that out.
Thanks for your feedback, you got me thinking again.
but it's strange, my first method works, the "loadStudentInfo" method.
I created the "matrix" array in the second method...I wonder if it would help if I created it in the main like I did with the first. I'll have to try that out.
Thanks for your feedback, you got me thinking again.
•
•
Join Date: Nov 2007
Location: Belgium
Posts: 62
Reputation:
Rep Power: 1
Solved Threads: 6
Your problem isn't in the fact that you create matrix inside that method. The problem is that you try to read from numberArray, which is still uninitialized at that moment. (You only gave values to a copy of numberArray in your first method, not the original array you are passing your second method.)
•
•
•
•
By giving your arrays to that method, you give them a local copy of the arrays.
This means that what ever you do inside that method, happens to the copy. In orde to get the result back into your initial arrays, you need to let your methods return the adjustments you made.
Actually, no, any operations he performs on the elements of the passed array do affect the original. It does not make a copy of the array.
rickster11,
The reason for the error is that you are not checking the length of the array in your loop, just for the end of file. You have declared the arrays to have five elements, but you are not hitting "eof" on the fifth read so it has incremented "x" and is trying to access an array element that doesn't exist. Make sure that your loop also checks against the array length to avoid this.
edit: Additionally, your "y" variable remains 0 as it is, so you will never have data past the first element in that dimension of your array.
Last edited by Ezzaral : Nov 28th, 2007 at 7:21 pm.
•
•
Join Date: Nov 2007
Location: Belgium
Posts: 62
Reputation:
Rep Power: 1
Solved Threads: 6
I watched the code again and Ezzaral is correct.
Proof for that is that you only go out of bounds at index 5. (If what I said was true, you should've been getting nullPointerExceptions, sorry about that
)
Proof for that is that you only go out of bounds at index 5. (If what I said was true, you should've been getting nullPointerExceptions, sorry about that
) ![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Can I ghost a RAID array??? (Windows NT / 2000 / XP / 2003)
- Creating dynamic array structures (C++)
- Array limit (C)
- struct dynamic 2d array alloc (C)
- string to integer array transformation (C)
- Array (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: Easy Derived Class Question
- Next Thread: Sorting



Linear Mode