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.

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

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.

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

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.

Darn, I keep getting the same outofbounds error.

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.

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 :))

Thanks a bunch guys, I was getting really frustrated. My book is horrible.

Once I started checking the array length in my loop, everything started to come together. Thanks for pointing out the problem with the 'y' variable as well, saved me some more time.

thanks again for the time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.