# 2 dimensional array storing help!

• January 31st, 2012, 12:58 PM
jts0541
2 dimensional array storing help!
My goal is to take the string and place each individual character in a two dimensional array. Here is what I have so far...

The string totalMaze is:
oooooooo#S#ooooooo#oo##oooo#ooo#oo#oo\$ooo######ooo oooooo

char [][] array = new char [numberOfRows][numberOfColumns];

for (int row = 0; row < numberOfRows; row++)
{

for (int col = 0; col < numberOfColumns; col++)
{

array[row][col] = totalMaze.charAt(?????????????);
}

}

I tried another for loop within the col loop, like for (int i = 0; i < totalmaze.length();i++)

the result is:

oooooooo
oooooooo
oooooooo
oooooooo
oooooooo
oooooooo
oooooooo

and I need it to be:

oooooooo
#S#ooooo
oo#oo##o
ooo#ooo#
oo#oo\$oo
o######o
oooooooo

ALSO...The goal of the project is to develop this maze with S as the start, o's as "roads" and #'s as "walls". \$ is the end.
We need to use a recursive method to find the treasure.

I need help developing the algorithm for finding the treasure. I need to check the neighbors, but not diagonals and mark with an X if it is not the treasure so that I don't check it again. Just hard to wrap my head around
• January 31st, 2012, 02:13 PM
marylandfour
Re: 2 dimensional array storing help!
Think about how a two dimensional array works, if you make a row then make columns within that specific row, what happens?

You don't need a third loop, and you're loop is in the right spot. Think about you're string. If you're dealing with a four character string ( let's say the word "Java") and you separate it into a two by two dimensional array. Row 1 Column 1 is the letter J, while Row 2 Column 1 is the letter A. Look at indexes the characters you're printing are. Tip : You're program needs to know how to adjust the index it needs to print for the next row.

Alright so I'm awful at recursion, but I would try and separate the recursive function into two smaller recursive functions. Think about the relationship between the neighbors. Hint: The left neighbor is -1 column away and the right neighbor is +1 column away.
• January 31st, 2012, 02:31 PM
jts0541
Re: 2 dimensional array storing help!
Yah i think I understand how a 2 dimensional array. But from your java array wouldn't that look like
J
a
v
a

Take the string javaprogramming for example I need it to be like

javaprog
ramming