This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Array out of bounds Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Array out of bounds" Watch "Array out of bounds" New topic

Array out of bounds

Karine Roy

Joined: Jul 11, 2011
Posts: 13
I am new at Java and I am not sure if I am posting at the right place. Please let me know if I am not. I am trying to transfer a 2 dimension array to a text file but get the following runtime error:

java.lang.ArrayIndexOutOfBoundsException: 2
at Parts.writeArray(
at Parts.main(

Here is the code I have so far:

Can someone please help me?

James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

The problem is due to the fact that your 2D array is not 'square' but the inner loop in this pair

assumes it is square. It is easily rectified. You just need to make sure that the inner loop indexes over the length of the row being processed i.e. parts[row].length and not parts.length .

Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
subject: Array out of bounds
jQuery in Action, 3rd edition