Hey guys
Icame across a weird situation wherein I had to use System.exit(1) in place of return,
although the output in both cases should be same, the output im getting with return statement is giving me the wrong result while the one with System.exit(1) is giving me the correct result ;

the program here uses recursion to solve the maze (0's are the paths, 1's are the walls) , the mouse is in index 91 of the 1 D array (we can use 2D array also , but i did this so as to avoid some unneccessary code), the exit is index 33;EVERY TIME THE METHOD EXITCELL IS CALLED: the mouse moves along the corresponding 0s; in case of the dead end it retraces its path to the entry point of the dead end, and then chosses another path

class MazeMouse{
	
	private int[] maze = {

	
	/*	0	1	2	3	4	5	6	7	8	9	10
	/*00*/	1,	 1,	 1, 	1, 	1, 	1, 	1, 	1, 	1, 	1, 	1,
	/*11*/	1,	  0, 	0, 	0 ,	0, 	0 ,	1, 	0 ,	0, 	0 ,	1,
	/*22*/	1,	  0 ,	1 ,	0 ,	0 ,	0 ,	1 ,	0 ,	1 ,	0 ,	1,
	/*33*/	0 ,	  0 ,	1 ,	0 ,	0 ,	0 ,	0 ,	0 ,	1 ,	0 ,	1,	
	/*44*/	1 ,	  0 ,	1 ,	1 ,	1 ,	1 ,	1 ,	0 ,	1 ,	0 ,	1,
	/*55*/	1 ,	  0 ,	1 ,	0 ,	1 ,	0 ,	0 ,	0 ,	1 ,	0 ,	1,
	/*66*/	1 ,	  0 ,	0 ,	0 ,	1 ,	0 ,	1 ,	0 ,	0 ,	0 ,	1,
	/*77*/	1 ,	 1  ,	1 ,	1 ,	1 ,	0,	 1 ,	0 ,	0 ,	0,	1,
	/*88*/	1 ,	  0 ,	1 ,	0 ,	1 ,	0 ,	1 ,	0 ,	0 ,	0 ,	1,
	/*99*/	1 ,	  0, 	0 ,	0 ,	0 ,	0 ,	1 ,	0 ,	0 ,	0 ,	1,
	/*110*/	1 ,	1 ,	1 ,	1, 	1 ,	1, 	1 ,	1, 	1 ,	1, 	1
	
		
	};
	private int start = 91;
	private int end = 33;
	private int moves[] = {-1,-11,1,11}; // left ,up ,right ,down;
	
	private int cameFrom[] = new int[121];
	
	public void exitcell(int current)
	{
		System.out.println(current+"   ");
		if(current == end){
				System.out.println("Exit Reached");
				System.exit(1);
				return ;
			            }
		for(int i = 0 ; i < 4 ; i++)
		{
			int next = current+moves[i];
			if(maze[next]!= 1 && cameFrom[current]!= next)
			{	
				
				cameFrom[next] = current;
				   exitcell(next);
				
			}
		}
		return;
	}	
		
	public static void main(String args[]){
		MazeMouse a = new MazeMouse();
		a.exitcell(a.start);	
		
	}
	
}

System exit would quit the program skipping bye all callers.
return goes back to the caller.

I haven't studied the code in detail, but I'm not surprised that the execution terminates differently with or without the System.exit.
With it the code terminates as soon as the exit square is found. Without it the current invocation returns to the the call on line 42, and the loop at line 35 continues to repeat and re-call exitcell, presumably ultimately backing out just as if the exit was a dead end.

Since System.exit prevents this method from being called in a useful way, you could do something like returning a maze array index value from each call. Return -1 normally, or the ( >= 0 ) index of the exit if found. Then the call on line 42 can be
int result = exitcell(next); if (result >= 0) return result;
which will back you out of the recursion stack tidyly.

System exit would quit the program skipping bye all callers.
return goes back to the caller.

really?... but logically.... shouldnt the method terminate with return as well?...
i mean ... at that stage... there shouldnt be anymore calls to exitcell(int).....

. at that stage... there shouldnt be anymore calls to exitcell(int).....

return just returns from that specific invocation of the method. Because it's recursive there will be many other active invocations of exitcell on the stack, which will not be terminated by any one return.

I haven't studied the code in detail, but I'm not surprised that the execution terminates differently with or without the System.exit.
With it the code terminates as soon as the exit square is found. Without it the current invocation returns to the the call on line 42, and the loop at line 35 continues to repeat and re-call exitcell, presumably ultimately backing out just as if the exit was a dead end.

Since System.exit prevents this method from being called in a useful way, you could do something like returning a maze array index value from each call. Return -1 normally, or the ( >= 0 ) index of the exit if found. Then the call on line 42 can be
int result = exitcell(next); if (result >= 0) return result;
which will back you out of the recursion stack tidyly.

i ve used a void exitcell(int) method here .... so can there be any way apart from the one you suggested, because ill have to change the method exitcell() in many ways...

thanks for the above i dea though... if nothing works ill have to use the one u suggested :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.