User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Nov 2007
Posts: 14
Reputation: rickster11 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rickster11 rickster11 is offline Offline
Newbie Poster

Array help

  #1  
Nov 28th, 2007
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 ();
		}
	}
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Belgium
Posts: 62
Reputation: Black Box is on a distinguished road 
Rep Power: 1
Solved Threads: 6
Black Box Black Box is offline Offline
Junior Poster in Training

Re: Array help

  #2  
Nov 28th, 2007
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
Last edited by Black Box : Nov 28th, 2007 at 5:04 pm. Reason: Added a little code to make clear what I meant
Reply With Quote  
Join Date: Nov 2007
Posts: 14
Reputation: rickster11 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rickster11 rickster11 is offline Offline
Newbie Poster

Re: Array help

  #3  
Nov 28th, 2007
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.
Reply With Quote  
Join Date: Nov 2007
Posts: 14
Reputation: rickster11 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rickster11 rickster11 is offline Offline
Newbie Poster

Re: Array help

  #4  
Nov 28th, 2007
Darn, I keep getting the same outofbounds error.
Reply With Quote  
Join Date: Nov 2007
Location: Belgium
Posts: 62
Reputation: Black Box is on a distinguished road 
Rep Power: 1
Solved Threads: 6
Black Box Black Box is offline Offline
Junior Poster in Training

Re: Array help

  #5  
Nov 28th, 2007
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.)
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 3,076
Reputation: Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold Ezzaral is a splendid one to behold 
Rep Power: 15
Solved Threads: 306
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Sensei

Re: Array help

  #6  
Nov 28th, 2007
Originally Posted by Black Box View Post
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.
Reply With Quote  
Join Date: Nov 2007
Location: Belgium
Posts: 62
Reputation: Black Box is on a distinguished road 
Rep Power: 1
Solved Threads: 6
Black Box Black Box is offline Offline
Junior Poster in Training

Re: Array help

  #7  
Nov 29th, 2007
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 )
Reply With Quote  
Join Date: Nov 2007
Posts: 14
Reputation: rickster11 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
rickster11 rickster11 is offline Offline
Newbie Poster

Re: Array help

  #8  
Nov 29th, 2007
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 9:15 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC