Results 1 to 9 of 9
Thread: Problem writing to a 2D array
- 08-10-2010, 04:12 PM #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 :
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[][].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(); 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; }
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:
Then it doesn't reproduce the file.Java Code:for (int a=0; a<maze.length; a++){ for (int b=0; b<maze.length; b++){ System.out.println(maze[a][b]); } }
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, 04:37 PM #2
Could you show what it outputs?it doesn't reproduce the file.
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, 04:47 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
This line is definitely incorrect:
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.Java Code:maze[b] = test;
kind regards,
Jos
- 08-10-2010, 04:50 PM #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.
- 08-10-2010, 04:57 PM #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?
- 08-10-2010, 05:07 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
Yup, that'll do, also, given a line (a String), you can set your matrix row as follows:
you can call that method for every line read.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; }
kind regards,
Jos
- 08-10-2010, 09:59 PM #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); 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, 04:58 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
- 08-11-2010, 09:31 AM #9
Member
- Join Date
- Aug 2010
- Posts
- 5
- Rep Power
- 0
Similar Threads
-
Writing and reading to/from a 2D array!
By celebor in forum New To JavaReplies: 11Last Post: 06-17-2010, 01:48 PM -
writing array values to another class
By ronald christian in forum New To JavaReplies: 27Last Post: 11-07-2008, 04:08 PM -
writing an array class
By wardd85 in forum New To JavaReplies: 5Last Post: 07-16-2008, 10:59 PM -
Writing a countdown array to a file.
By kewlgeye in forum New To JavaReplies: 6Last Post: 05-25-2008, 06:09 AM -
Reading/Writing a File using byte array
By Java Tip in forum Java TipReplies: 0Last Post: 01-16-2008, 10:41 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks