# Thread: Problem writing to a 2D array

1. Member Join Date
Aug 2010
Posts
5
Rep Power
0

## Problem writing to a 2D array

All,

I want to read a file and use to create a maze.
The file is as follows:

1111111111
1000000001
1000000001
1000000001
1000000001
1111100111
1000000001
1000000001
1000000001
1111111111

Where 1 is supposed to be a wall, and 0 free space.

I solved the issue with a simple scannermethod, but now I want to use a filereader.

The code :
Java Code:
```public int[][] readMaze() throws Exception
{
File file = new File ("mazeFile.txt.txt");
Scanner sc = new Scanner (file);
String nl;
nl = sc.nextLine();
kol = nl.length();
sc.close();

int[][] maze = new int[kol][kol];
int[] test = new int[kol];
int counter = 0;

for (int a=0; a<kol; a++){
for (int b=0; b<kol; b++){
if (r == 48 || r == 49){
switch (r){
case 48: test[b] = 0; break;
case 49: test[b] = 1; break;
}
counter++;
if (counter > (kol-1)){
fr.skip(2);
maze[b] = test;
counter = 0;
}
}
}
}
return maze;
}```
So, the first scanner is to determine what the amount of columns or rows is. It is a square, so thats why I use kol two times in the declaration of maze[][].
When using a filereader, it returns ASCII values. So, if a 48 or 49 is returned, then it has to save 1 or 0 in an array test[]. For every 10 ones or zeros, it had to produce 1 array that saves it into maze[][]. And with that, it skips reader EOL and carriage return. Thats how you get a 2d-maze.

But, when I want to check the contents of maze with:
Java Code:
```for (int a=0; a<maze.length; a++){
for (int b=0; b<maze.length; b++){
System.out.println(maze[a][b]);
}
}```
Then it doesn't reproduce the file.

I read all about 2d arrays everywhere, but I can't get a clue about what is wrong. I think it goes wrong at the maze[b] = test;.

What goes wrong?  Reply With Quote

2. ## it doesn't reproduce the file.
Could you show what it outputs?

An easy way to view a one dim array's contents is to use:
println("TheArray=" + Arrays.toString(THEARRAY));

Instead of using 48 and 49 you could use '0' and '1' which would be more readable.  Reply With Quote

3. ## This line is definitely incorrect:

Java Code:
` maze[b] = test;`
and for several reasons: you set the row of the matrix to the array 'test' over and over again. (check your indexes and you'll see) and you set every row to the same array 'test' over and over again so all rows of your matrix end up pointing to the same array 'test'. My suggestion is to read a line of text and fill the original rows of the matrix with it. Also don't skip 2 characters in your input stream, that'd only be valid on MS Windows machines. Read lines and fill row after row in your original matrix.

kind regards,

Jos  Reply With Quote

4. Member Join Date
Aug 2010
Posts
5
Rep Power
0

## Yes, it gives:
0
0
...
0
0
1
1
...
1
1
So, 90 times a zero and at the end 10 times a one.  Reply With Quote

5. Member Join Date
Aug 2010
Posts
5
Rep Power
0

## So that means I have to use a buffered reader? Because you can't read a complete line at once with a filereader, do you?  Reply With Quote

6. ##  Originally Posted by skelter15 So that means I have to use a buffered reader? Because you can't read a complete line at once with a filereader, do you?
Yup, that'll do, also, given a line (a String), you can set your matrix row as follows:

Java Code:
```private void fillRow(int[] row, String line) {
for (int i= 0; i < line.length(); i++)
row[i]= (line.charAt(i) == '1')?1:0;
}```
you can call that method for every line read.

kind regards,

Jos  Reply With Quote

7. Member Join Date
Aug 2010
Posts
5
Rep Power
0

## Jos, thanks for your code. But i am not quite sure what to do with it :confused:

I tried:
Java Code:
```FileReader fr = new FileReader (file);
int[][] maze = new int[kol][kol];
int[] test = new int[kol];
String line = "";
int counter = 0;

for (int a=0; a<kol; a++){
fillRow (test, line);
maze[a] = test;
}
return maze;```  Reply With Quote

8. ##  Originally Posted by skelter15 Jos, thanks for your code. But i am not quite sure what to do with it :confused:

I tried:
Java Code:
```FileReader fr = new FileReader (file);
int[][] maze = new int[kol][kol];
int[] test = new int[kol];
String line = "";
int counter = 0;

for (int a=0; a<kol; a++){
fillRow (test, line);
maze[a] = test;
}
return maze;```
Almost right; my method fills a row so why not pass it a row of your maze? Like this:

Java Code:
`	    	   fillRow (maze[a], line);`
kind regards,

Jos  Reply With Quote

9. Member Join Date
Aug 2010
Posts
5
Rep Power
0

## Oh, thats an easy solution. :) It works! Thanks!  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•