9 Replies - 1497 Views - Last Post: 07 December 2012 - 09:10 PM Rate Topic: -----

#1 AceWindgar1285  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 12-October 12

having a java.lang.ArrayIndexOutOfBoundsException-1

Posted 07 December 2012 - 06:37 AM

I'm working on a recursion for sloving a maze and i keep getting this error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Maze.findSolution(Maze.java:41)
at Maze.solution(Maze.java:64)
at Main3.main(Main3.java:21)
public class Maze {
	// Class Variables
	final static char BLANK = ' ';
	final static char WALL = 'X';
	final static char BACK = 'B';
	final static char PATH = '-';
	static char[][] map=null;
	
	Maze(String [] n)
	{
		map= new char[n.length][];
		for(int i =0;i<n.length;i++){
			map[i] = n[i].toCharArray();
		}
			
	}
	
	public boolean findSolution(int row, int col)
	{
		//Determines if out of bounds
		if(row<0 || col<0 || row>=map.length || col>=map.length)
		{
			return false;
		}
		if(col==map.length)
		{
			return true;
		}
		if (map[row][col]==WALL)
		{
			return false;
		}
			map[row][col]=PATH;

			if(map[row][col+1]==BLANK)
			{
				findSolution(row,col+1);
				return true;
			}
			if(map[row-1][col]==BLANK)
			{
				findSolution(row-1,col);
				return true;
			}
			if(map[row][col-1]==BLANK)
			{
				findSolution(row,col-1);
				return true;
			}
			if(map[row+1][col]==BLANK)
			{
				findSolution(row+1,col);
				return true;
			}
			else{	
		map[row][col] = BACK;
		return false;}
		
	}
	
	public void solution()
	{
		findSolution(0,5);
	}
	
	public void displayMaze()
	{
		final char[ ][ ] maze = map;
		for(int i = 0; i < maze.length; i++){
			for(int j = 0; j < maze.length; j++)
				System.out.print("|"+maze[i][j]);
			System.out.println("|");
		}
	}
}



public class Main3 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Maze maze1 = new Maze( new String[]
							{"xxxxxxxxxxxx",
							 "x xxxxxxxxxx",
							 "x   xxxxxx  ",
							 "xxx x   xx x",
							 "    x x xx x",
							 "x xxx  xx  x",
							 "x  xxx xx xx",
							 "xx     x  xx",
							 "xx xxxxx xxx",
							 "xx   x   xxx",
							 "xxxx   xxxxx",
							 "xxxxxxxxxxxx"});
		maze1.solution();
		maze1.displayMaze();

	}

}



any help would be greatly appericated

Is This A Good Question/Topic? 0
  • +

Replies To: having a java.lang.ArrayIndexOutOfBoundsException-1

#2 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: having a java.lang.ArrayIndexOutOfBoundsException-1

Posted 07 December 2012 - 07:52 AM

that is because you are walking off the edge of the map. Look at the your code for when row = 0. When you get to line 40, you try and make this call:
 if(map[row-1][col]==BLANK)

that means your looking for map[-1][col]. Java cannot access an index that is less than zero or >= the array.Length.
Was This Post Helpful? 0
  • +
  • -

#3 AceWindgar1285  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 12-October 12

Re: having a java.lang.ArrayIndexOutOfBoundsException-1

Posted 07 December 2012 - 10:30 AM

so what should do to fix my issue?
Was This Post Helpful? 0
  • +
  • -

#4 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: having a java.lang.ArrayIndexOutOfBoundsException-1

Posted 07 December 2012 - 12:00 PM

You have two options.

1. You can surround your code with a try catch block. Catching the ArrayIndexOutOfBoundsException, you know you will be returning false.

2. Refactor your logic. Add some more if/else statements when you are determining the bounds


However you really shouldn't be checking what the neighbor's character is. You should just call the method on the neighbor and have it report false if its a bad step. (thinking this way will make your logic easier too).
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: having a java.lang.ArrayIndexOutOfBoundsException-1

Posted 07 December 2012 - 12:07 PM

View PostSwiftStriker00, on 07 December 2012 - 03:00 PM, said:

1. You can surround your code with a try catch block. Catching the ArrayIndexOutOfBoundsException, you know you will be returning false.

this is a very very bad coding practice.
In that case start all your method by a try {
till the end of it with the catch(Throwable e) {}
as the last statement of your methjod

Try/catch are for situations where you don't have the control. Not to hide bad coding
Was This Post Helpful? 0
  • +
  • -

#6 SwiftStriker00  Icon User is offline

  • No idea why my code works
  • member icon

Reputation: 433
  • View blog
  • Posts: 1,596
  • Joined: 25-December 08

Re: having a java.lang.ArrayIndexOutOfBoundsException-1

Posted 07 December 2012 - 12:25 PM

Thanks for the expansion on that pbl. I had to run off to the meeting, otherwise I would have made that clarification.

Its also slower so, AceWindgar1285 you should be using my second option
Was This Post Helpful? 0
  • +
  • -

#7 AceWindgar1285  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 12-October 12

Re: having a java.lang.ArrayIndexOutOfBoundsException-1

Posted 07 December 2012 - 01:12 PM

is it possible to see a sample of what you would perfer me to do just a small sample to put me in the right direction?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: having a java.lang.ArrayIndexOutOfBoundsException-1

Posted 07 December 2012 - 01:19 PM

    if( col < map[row].length - 1 && map[row][col+1]==BLANK)  
    {
       ...

    if(row > 0 && map[row-1][col]==BLANK)
    {  
       ...




P.S.

When I do stuff like that I prefer creating an array of Point that I call neighbour an that create at initialisation

so instead of testing all the bounds each time aI test for the elements in that array
Was This Post Helpful? 0
  • +
  • -

#9 AceWindgar1285  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 12-October 12

Re: having a java.lang.ArrayIndexOutOfBoundsException-1

Posted 07 December 2012 - 04:20 PM

iactually figured out what i was doing wrong i was having it start at the wrong point. But now i can't get it to backtrack.
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: having a java.lang.ArrayIndexOutOfBoundsException-1

Posted 07 December 2012 - 09:10 PM

:^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1