# Problem writing to a 2D array

• 08-10-2010, 05:12 PM
skelter15
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 :
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();                         FileReader fr = new FileReader (file);           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++){                                    int r = fr.read();                                   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:
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?
• 08-10-2010, 05:37 PM
Norm
Quote:

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.
• 08-10-2010, 05:47 PM
JosAH
This line is definitely incorrect:

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
• 08-10-2010, 05:50 PM
skelter15
Yes, it gives:
0
0
...
0
0
1
1
...
1
1
So, 90 times a zero and at the end 10 times a one.
• 08-10-2010, 05:57 PM
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?
• 08-10-2010, 06:07 PM
JosAH
Quote:

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:

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
• 08-10-2010, 10:59 PM
skelter15
Jos, thanks for your code. But i am not quite sure what to do with it :confused:

I tried:
Code:

```FileReader fr = new FileReader (file);               BufferedReader br = new BufferedReader (fr);               int[][] maze = new int[kol][kol];               int[] test = new int[kol];               String line = "";               int counter = 0;               for (int a=0; a<kol; a++){                        line = br.readLine();                       fillRow (test, line);                       maze[a] = test;               }           return maze;```
• 08-11-2010, 05:58 AM
JosAH
Quote:

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

I tried:
Code:

```FileReader fr = new FileReader (file);               BufferedReader br = new BufferedReader (fr);               int[][] maze = new int[kol][kol];               int[] test = new int[kol];               String line = "";               int counter = 0;               for (int a=0; a<kol; a++){                        line = br.readLine();                       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:

Code:

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

Jos
• 08-11-2010, 10:31 AM
skelter15
Oh, thats an easy solution. :) It works! Thanks!