aspose file tools*
The moose likes Beginning Java and the fly likes Storing a text file into a 3d Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Storing a text file into a 3d Array" Watch "Storing a text file into a 3d Array" New topic
Author

Storing a text file into a 3d Array

Johnny Peterson
Greenhorn

Joined: Sep 11, 2012
Posts: 6
Hi all,

I have the following task : I am supposed to create a maze from a text file, the maze can be multiple levels. A # is a wall, a . is a space, a @ is start, a * is an exit, and a ^ signifies a stair, which leads to a different level of the maze. I am supposed to determine the dimensions of the maze and store them into an array, as well as populate the array with the character at the given location. I am having trouble with the last part, and I would appreciate any help. Here is what I have so far :



Here is what I get when I compile :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at Maze.main(Maze.java:65)

So my problem lies in the for loop, but I am unsure why it is giving me that error. I checked the height, width, and depth numbers, and they all are correct. Any hints or tips will be appreciated, I seem to be stuck on this. Also, here is the text file I am using :

#######
#...#@#
#^#...#
#######
-------
#######
#...###
#^#.*##
#######

Thanks in advance!

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Welcome to Javaranch, Johnny.

Yes the issue in the for loop. Note that array indices start from [0] to [array's length - 1]. So the char [height][width][depth] array can hold char[0][0][0] to char [height-1][width-1][depth-1] items (since index starts from 0).

Since you are not looping from 0 to length - 1, but directly try to assign to array's length, dimensions[height][width] you receive the ArrayIndexOutOfBoundsException.

I shall move the thread to Beginning Java forum.
Johnny Peterson
Greenhorn

Joined: Sep 11, 2012
Posts: 6
John Jai wrote:Welcome to Javaranch, Johnny.

Yes the issue in the for loop. Note that array indices start from [0] to [array's length - 1]. So the char [height][width][depth] array can hold char[0][0][0] to char [height-1][width-1][depth-1] items (since index starts from 0).

Since you are not looping from 0 to length - 1, but directly try to assign to array's length, dimensions[height][width] you receive the ArrayIndexOutOfBoundsException.

I shall move the thread to Beginning Java forum.


Thanks for the help, but when I do the following :



I still get the same error, which confuses me.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11240
    
  16

System.out.println() is your best friend. I would stick some in just before line 65, so you have something like this:



i suppose you could print height and width before entering the loop on line 60, which would reduce the noise a little.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Johnny Peterson
Greenhorn

Joined: Sep 11, 2012
Posts: 6
fred rosenberger wrote:System.out.println() is your best friend. I would stick some in just before line 65, so you have something like this:



i suppose you could print height and width before entering the loop on line 60, which would reduce the noise a little.


Yes, that was a good tip of you. I got the height and the width right, but after it shows that i is 0 and j is 1, it displays the same error, which is weird, it does not store anything into the dimensions array. I think that maybe I am not saving the characters in the file correctly?
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Issue is here - dimensions[height][width] - As arrays have the index from 0 to length -1. Since you have declared dimensions[height]... you can use only from dimensions[0] till dimensions[height-1]. Read further in the array tutorials
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Storing a text file into a 3d Array