Results 1 to 9 of 9
  1. #1
    jubbiejub is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default multi-dimensional array not printing as expected

    I'm designing a program that uses a recursive algorithm to make an explorer find his way out of a maze. I'm just getting started on it, but for some reason when I attempt to print the maze using my displayMaze method, it isn't correctly printing out the right wall.

    Here's the maze class:

    Java Code:
    public class Maze {
    
    	private final int rows = 15;
    	private final int columns = 15;
    	private char[][] maze = new char[rows][columns];
    	
    	public void createMaze(char[][] x){
    		//method to create a maze out of a multi-dimensional character array
    		//preconditions: none
    		//postconditions: Assigns a character H to all boundaries and X to the end of maze 
    		for(int i = 0; i < x.length; ++i){
    			x[i][0] = 'H';
    			x[0][i] = 'H';
    			x[i][x.length - 1] = 'H';
    			x[x.length - 1][i] = 'H';
    		}
    		
    	}//end of createMaze	
    
    
    	public void displayMaze(char[][] x){
    		//method to print out the maze
    		//preconditions: character array must have values 
    		//postconditions: prints out the maze
    		for(int i = 0; i < rows; i++){
    			
    			for (int j = 0; j < columns; j++) {
    				System.out.print(" " + x[i][j]);
    			}	
    				System.out.println();
    		}
    		
    	}//end of displayMaze
    	
    	
    	
    	public static void main(String[] args){
    		
    		Maze jaron = new Maze();
    		jaron.createMaze(jaron.maze);
    		jaron.displayMaze(jaron.maze);
    	}//end of main
    	
    
    }//end of class
    The output of this code is currently:

    H H H H H H H H H H H H H H H
    H
    H
    H
    H
    H
    H
    H
    H
    H
    H
    H
    H
    H
    H H H H H H H H H H H H H H H
    I have printed out the value of maze[1][14], etc, and it prints 'H' so I know the value is being assigned correctly. The error is in the display method.

    Anybody have any ideas?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: multi-dimensional array not printing as expected

    I see the right wall when I run the program. Make sure you are running the most recent version: ie delete the Maze.class file then recompile.

    -----

    Why are you passing the maze argument around like that? The whole idea of making it private it that is not directly accessed by any users of the class. Put main() in a different class and you'll see that it cannot create or print the maze in the way you have written it.

    A better approach would be to have the public methods - createMaze() and displayMaze() - take no arguments. Indeed the code currently in createMaze() could go in the Maze class constructor.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: multi-dimensional array not printing as expected

    Interesting. When I run the same code you've posted, I get a square border of 'H'.

  4. #4
    jubbiejub is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: multi-dimensional array not printing as expected

    That's a really good idea. I know it's bad practice but I never really consider what I could put in the constructor. I'll definitely put the createMaze code in the constructor.

    I am running in Eclipse, so all there is in the directory at the moment is Maze.java (no class file) how could I go about making sure it's updated?

  5. #5
    jubbiejub is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: multi-dimensional array not printing as expected

    Now I am getting an error that won't let me compile the code and is completely wrong. I changed the displayMaze method to take no arguments, but now in main it won't let me compile because it isn't taking a char[][] argument :/

    I don't think Eclipse is working correctly at the moment.

    EDIT: I am having to make an entirely new class everytime I want the code to update. Now that I have moved all the new code to a different class, is compiles and runs fine, yet there is still no right wall.
    Last edited by jubbiejub; 05-05-2012 at 06:11 AM.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: multi-dimensional array not printing as expected

    One step at a time. Get the program to run showing the right wall *before* making changes to the code. Eclipse has a "Clean..." item in the Project menu that might help. IDEs like Eclipse have a reasonably complicated notion of "project" that determines what gets compiled and run when you click the various buttons. That (additional complexity not directly related to Java or your code) is one reason why many advise getting used to compiling and running outside of the IDE framework. Another is that for communicating on forums like this it can be easier to completely describe a problem when it doesn't involve the setup and configuration of the IDE as well as the code.

    -----

    One you can see the right wall, work on refactoring the code. Clearly it's not enough to remove the arguments from the method definitions: you will also have to make slight changes to the code in those methods.

  7. #7
    jubbiejub is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: multi-dimensional array not printing as expected

    I thought we had determined the fact that it isn't a problem with the code, instead it is problem with my Eclipse set-up or something else on my side. It has run as intended for both you and Fubrable so something isn't working correctly in Eclipse. I'll open up the Terminal and compile/run it there.

    EDIT: What the hell... Check out what I get when ran in Ubuntu 11.10 terminal:

    Java Code:
     
     H H H H H H H H H H H H H H H
     H              H
     H              H
     H              H
     H              H
     H              H
     H              H
     H              H
     H              H
     H              H
     H              H
     H              H
     H              H
     H              H
     H H H H H H H H H H H H H H H
    EDIT EDIT: The right side of H's are supposed to be displayed right in the middle of the display, halfway to where it should be. The quote function isnt displaying it right.
    Last edited by pbrockway2; 05-05-2012 at 06:42 AM. Reason: quote->code

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: multi-dimensional array not printing as expected

    It turns out that the output is different with NetBeans, Eclipse, Ubuntu terminal and the Windows command line.

    This is not too surprising when you think about it. At the moment you are printing all 255 characters of the array with spaces between each and the next character in the same line. The "edges" of the array are the character 'H', but what are the other characters? And how would you expect them to be rendered?

    It might be safer if you fill the array with spaces (character ' ') before assigning the 'H' characters. At least spaces will be rendered in a consistent way: providing you (ie whatever is displaying the output) are using a fixed width font.

  9. #9
    jubbiejub is offline Member
    Join Date
    Mar 2012
    Posts
    16
    Rep Power
    0

    Default Re: multi-dimensional array not printing as expected

    I was messing around with the code before I went out earlier, and I did exactly what you just suggested. It works perfectly on the Ubuntu terminal but I still haven't tested it in Eclipse. (I'm assuming it'll work fine)

    Here's the code I used to solve the problem:

    Java Code:
    import java.util.Scanner;
    
    public class Maze {
    
    	private Scanner keyboard = new Scanner(System.in);
    	private int input = keyboard.nextInt();
    	private final int rows = input;
    	private final int columns = input;
    	private char[][] maze = new char[rows][columns];
    	
    		
    	public Maze(){
    		//default constructor that creates a maze of user entered size
    		//
    		for(int i = 0; i < maze.length; ++i){
    			maze[i][0] = 'H';
    			maze[0][i] = 'H';
    			maze[i][maze.length - 1] = 'H';
    			maze[maze.length - 1][i] = 'H';
    			
    		}
    		for(int x = 1; x < maze.length - 1; ++x){
    			
    			for(int j = 1; j < maze.length - 1; ++j){
    			maze[x][j] = ' ';
    			}
    		}
    		
    		maze[maze.length - 3][maze.length - 1] = 'X';
    	}
    
    	public void displayMaze(){
    		//method to print out the maze
    		//preconditions: character array must have values 
    		//postconditions: prints out the maze
    		for(int i = 0; i < rows; i++){
    			
    			for (int j = 0; j < columns; j++) {
    				System.out.printf("%c ", maze[i][j]);
    			}	
    				System.out.println();
    		}
    		
    	}//end of displayMaze
    	
    	
    	
    	public static void main(String[] args){
    		
    		Maze jaron = new Maze();
    		jaron.displayMaze();
    	}//end of main
    	
    
    }//end of class

Similar Threads

  1. multi or two dimensional array
    By maya700 in forum New To Java
    Replies: 4
    Last Post: 07-12-2010, 07:52 PM
  2. Multi-dimensional array program-help!
    By bobmasta5 in forum New To Java
    Replies: 13
    Last Post: 03-15-2009, 08:21 PM
  3. Multi-dimensional array
    By VinTiger in forum New To Java
    Replies: 22
    Last Post: 03-01-2009, 07:51 AM
  4. Multi dimensional Array
    By Preethi in forum New To Java
    Replies: 1
    Last Post: 07-09-2008, 04:34 PM
  5. Help with array multi-dimensional
    By barney in forum New To Java
    Replies: 1
    Last Post: 07-31-2007, 09:00 PM

Posting Permissions

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