• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • paul wheaton
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • Mikalai Zaikin
  • Himai Minh

Storing a text file into a 3d Array

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
lowercase baba
Posts: 12982
66
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Johnny Peterson
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 want my playground back. Here, I'll give you this tiny ad for it:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic