Results 1 to 8 of 8
  1. #1
    evelina1 is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default Really stuck in java

    Hi..I am pretty new to JAVA and im suppose to write a code that generates a maze...i have the code and i run it and i keep getting arrayoutofbound exception..here is the code.


    Java Code:
    public class StudentMazeGenerator
    implements MazeGenerator
    {
    	private static final int h2 = 0;
    	private static final int w2 = 0;
    	private boolean[][] currMazeLoc;
    	private boolean visited;
    	//private Object randomNumInt;
    	int[] listArray = {0,1,2,3};
    
    
    
    	public  Maze generateMaze(int width, int height, MazeGeneratorListener listener)
    	{
    		Maze mazeFactory = MazeFactory.createMaze(width, height);
    
    		currMazeLoc = new boolean[width][height]; 
    		generatePath(mazeFactory, width, height, listener);
    
    		return mazeFactory;
    	}
    
    	public void generatePath(Maze maze, int w, int h, MazeGeneratorListener  listener)
    	{
    		//	int[] listArray = new int[] {0,1,2,3};
    		currMazeLoc[w][h] = visited;
    		listener.mazeModified(maze);
    		while(!unCheckedCell(w, h))
    		{
    			Random random = new Random();
    			int temp = random.nextInt();
    			if(temp == 0 && h != 0)
    			{
    				//check inside each of them to make sure ur not goin out of bound
    				//ur at the end of the maze and u want to go east there is no more room...u get arrayindexoutofboundException
    				if(!(currMazeLoc[w][h-1]))
    				{
    					maze.removeWallNorthOf(w, h); 
    					h--;  
    					listener.mazeModified(maze);
    					generatePath(maze, w, h ,listener);  
    				}  
    			}  
    			if(temp == 1 && w != w2 - 1)  
    			{   
    				if(!currMazeLoc[w+1][h])  
    				{  
    					maze.removeWallSouthOf(w, h);  
    					h++;  
    					listener.mazeModified(maze);
    					generatePath(maze, w, h ,listener);  
    				}  
    			}  
    			if(temp == 2 && h!= h2 - 1)  
    			{    
    				if(!currMazeLoc[w][h+1])  
    				{  
    					maze.removeWallEastOf(w, h);  
    					h++;  
    					listener.mazeModified(maze);
    					generatePath(maze, w, h ,listener);    
    				}  
    			}  
    			if(temp == 3 && w != 0)  
    			{  
    				if(!currMazeLoc[w-1][h])  
    				{  
    					maze.removeWallWestOf(w,h); 
    					h--;  
    					listener.mazeModified(maze);
    					generatePath(maze, w, h ,listener);    
    				}  
    			}        
    			return;  	
    		}
    	}
    	//intArray.length-1;
    	//int temp = listArray[i]
    	//create an array of the (0 to 3) for N, S , E, W
    	//create a method that shuffles the array N, E, S, W
    	public void shuffle()
    	{
    		Random random = new Random();
    		int[] listArray = new int[] {0,1,2,3};
    		for (int i=0; i<listArray.length; i++)
    		{
    			int randomNum = random.nextInt(listArray.length -1);
    			int temp = listArray[i];
    		}
    	}
    
    
    
    
    	public boolean unCheckedCell(int w1, int h1) {
    		if (( w1 == 0) && (h1 == 0))
    			return (currMazeLoc[w1+1][h1] && currMazeLoc[w1][h1+1]);
    		else if((w1 == (w2 - 1)) && (h1 == (h2 - 1)))  
    			return (currMazeLoc[w1-1][h1] && currMazeLoc[w1][h1-1]); 
    		else if((w1 == 0) && (h1 == (h2 - 1)))
    			return (currMazeLoc[w1][h1-1] && currMazeLoc[w1+1][h1]); 
    		else if (w1 == (w2 - 1) && (h1 == 0))
    			return (currMazeLoc[w1 -1][h1] && currMazeLoc[w1] [h1 +1]);
    		else
    			return (currMazeLoc[w1+1][h1] && currMazeLoc[w1][h1+1] && currMazeLoc[w1-1][h1] && currMazeLoc[w1][h1-1]); 
    		//return false;
    	}	       
    
    	public int nextInt(int n)  
    	{  
    		return (int)(0 + Math.random() * n);  
    	}  
    
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Post the exact error message please. Stack traces are helpful, they tell you what part of the code threw an exception/caused an error. Somewhere you are indexing into an array above the length, or below 0.

    Java Code:
    public int nextInt(int n)  
    	{  
    		return (int)(0 + Math.random() * n);  
    	}
    Also in this piece of code, there is no point to add 0. .5555555555555+ 0 is still the same, it can be changed to
    Java Code:
    return (int)(Math.random()*n);

  3. #3
    evelina1 is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    Exception in thread "Thread-4" java.lang.ArrayIndexOutOfBoundsException: 10
    at StudentMazeGenerator.generatePath(StudentMazeGener ator.java:39)
    at StudentMazeGenerator.generateMaze(StudentMazeGener ator.java:31)
    at MazeFrame$MazeGenerationThread.run(MazeFrame.java: 275)

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

    Default

    java.lang.ArrayIndexOutOfBoundsException: 10
    at StudentMazeGenerator.generatePath(StudentMazeGener ator.java:39)
    Look at the code at line 39. The value of the index in the array used on that line is 10 but the array does not have 11 elements. Check your logic to see how the index got past the end of the array.

    Add some printlns to show the index values as the code executes if you need to see when the indexes go bad.

  5. #5
    evelina1 is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    So i made some changes..since i had my width and height mixed up in some places but i still get out of bound exception.

    Java Code:
    public class StudentMazeGenerator
    implements MazeGenerator
    {
    	private static final int h2 = 0;
    	private static final int w2 = 0;
    	private boolean[][] currMazeLoc;
    	private boolean visited;
    	//private Object randomNumInt;
    	int[] listArray = {0,1,2,3};
    
    
    
    	public  Maze generateMaze(int width, int height, MazeGeneratorListener listener)
    	{
    		Maze mazeFactory = MazeFactory.createMaze(width, height);
    
    		currMazeLoc = new boolean[width][height]; 
    		generatePath(mazeFactory, width, height, listener);
    
    		return mazeFactory;
    	}
    
    	public void generatePath(Maze maze, int currX, int currY, MazeGeneratorListener  listener)
    	{
    		//	int[] listArray = new int[] {0,1,2,3};
    		currMazeLoc[currX][currY] = visited;
    		listener.mazeModified(maze);
    		while(!unCheckedCell(currX, currY))
    		{
    			Random random = new Random();
    			int temp = random.nextInt();
    			if(temp == 0 && currY != 0)
    			{
    				//check inside each of them to make sure ur not goin out of bound
    				//ur at the end of the maze and u want to go east there is no more room...u get arrayindexoutofboundException
    				if(!(currMazeLoc[currX][currY-1]))
    				{
    					maze.removeWallNorthOf(currX, currY); 
    					//h--;  
    					listener.mazeModified(maze);
    					generatePath(maze, currX, currY-1 ,listener);  
    				}  
    			}  
    			if(temp == 1 && currY != 9)  
    			{   
    				//if ur on south u can go as far 9 so u need to check ur y coordinate to make sure ur not going more than the size.
    				if(!currMazeLoc[currX][currY+1]) 
    				{  
    					maze.removeWallSouthOf(currX, currY);  
    					//h++;  
    					listener.mazeModified(maze);
    					generatePath(maze, currX, currY+1 ,listener);  
    				}
    
    			}  
    			if(temp == 2 && currX != 9)  
    			{    
    				if(!currMazeLoc[currX+1][currY])  
    				{  
    					maze.removeWallEastOf(currX, currY);  
    					//h++;  
    					listener.mazeModified(maze);
    					generatePath(maze, currX+1, currY ,listener);    
    				}  
    			}  
    			if(temp == 3 && currX != 0)  
    			{  
    				if(!currMazeLoc[currX-1][currY])  
    				{  
    					maze.removeWallWestOf(currX,currY); 
    					//h--;  
    					listener.mazeModified(maze);
    					generatePath(maze, currX-1, currY ,listener);    
    				}  
    			}        
    			return;  	
    		}
    	}
    	//intArray.length-1;
    	//int temp = listArray[i]
    	//create an array of the (0 to 3) for N, S , E, W
    	//create a method that shuffles the array N, E, S, W
    	public void shuffle()
    	{
    		Random random = new Random();
    		int[] listArray = new int[] {0,1,2,3};
    		for (int i=0; i<listArray.length; i++)
    		{
    			int randomNum = random.nextInt(listArray.length -1);
    			int temp = listArray[i];
    		}
    	}
    
    	public boolean unCheckedCell(int w1, int h1) {
    		if (( w1 == 0) && (h1 == 0))
    			return (currMazeLoc[w1+1][h1] && currMazeLoc[w1][h1+1]);
    
    		else if((w1 == (w2 - 1)) && (h1 == (h2 - 1)))  
    			return (currMazeLoc[w1-1][h1] && currMazeLoc[w1][h1-1]); 
    
    		else if((w1 == 0) && (h1 == (h2 - 1)))
    			return (currMazeLoc[w1][h1-1] && currMazeLoc[w1+1][h1]); 
    
    		else if (w1 == (w2 - 1) && (h1 == 0))
    			return (currMazeLoc[w1 -1][h1] && currMazeLoc[w1] [h1 +1]);
    
    		else
    			return (currMazeLoc[w1+1][h1] && currMazeLoc[w1][h1+1] && currMazeLoc[w1-1][h1] && currMazeLoc[w1][h1-1]); 
    		//return false;
    	}	       
    
    	public int nextInt(int n)  
    	{  
    		return (int)(Math.random()*n); 
    	}  
    
    }

    error i get:

    Exception in thread "Thread-3" java.lang.ArrayIndexOutOfBoundsException: 10
    at StudentMazeGenerator.generatePath(StudentMazeGener ator.java:39)
    at StudentMazeGenerator.generateMaze(StudentMazeGener ator.java:31)
    at MazeFrame$MazeGenerationThread.run(MazeFrame.java: 275)

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

    Default

    Did you add printlns to show the indexes used on line 39?
    The print out could help you fix your problem.

  7. #7
    evelina1 is offline Member
    Join Date
    Jul 2011
    Posts
    5
    Rep Power
    0

    Default

    do i add that after the line 39...where do i have to do printlns?

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

    Default

    You need to print the values out BEFORE they cause the exception so you can see them.

Similar Threads

  1. help..stuck w/ a java programming assignment
    By clemsontigers in forum New To Java
    Replies: 16
    Last Post: 02-19-2014, 04:16 AM
  2. Java Help please xD SO stuck
    By Ethan in forum New To Java
    Replies: 1
    Last Post: 03-19-2011, 12:54 PM
  3. New to java and stuck
    By xpd259 in forum New To Java
    Replies: 2
    Last Post: 12-12-2010, 09:34 AM
  4. Stuck new to Java
    By Jaguar1998 in forum New To Java
    Replies: 3
    Last Post: 04-09-2010, 12:02 PM
  5. Kinda stuck of learning Java
    By jurka in forum New To Java
    Replies: 2
    Last Post: 02-14-2009, 04: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
  •