Results 1 to 9 of 9
  1. #1
    skelter15 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default 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();
    	       
    	   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:
    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?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default

    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.

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    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

  4. #4
    skelter15 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

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

  5. #5
    skelter15 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    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?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by skelter15 View Post
    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

  7. #7
    skelter15 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

    Default

    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;

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,352
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by skelter15 View Post
    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;
    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

  9. #9
    skelter15 is offline Member
    Join Date
    Aug 2010
    Posts
    5
    Rep Power
    0

Similar Threads

  1. Writing and reading to/from a 2D array!
    By celebor in forum New To Java
    Replies: 11
    Last Post: 06-17-2010, 01:48 PM
  2. writing array values to another class
    By ronald christian in forum New To Java
    Replies: 27
    Last Post: 11-07-2008, 04:08 PM
  3. writing an array class
    By wardd85 in forum New To Java
    Replies: 5
    Last Post: 07-16-2008, 10:59 PM
  4. Writing a countdown array to a file.
    By kewlgeye in forum New To Java
    Replies: 6
    Last Post: 05-25-2008, 06:09 AM
  5. Reading/Writing a File using byte array
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-16-2008, 10:41 AM

Posting Permissions

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