Can't solve it?

hey guys,

i posted the actual question in other thread but now i'm countering a syntax type of error and i don't know what's wrong.

Those of you who don't know about the actual problem, here it is: I need to find the shortest possible path from one 'X' to another 'X' and 'A's representing the blocks.

Please help? Thanks in advance! =]

Code:

import java.util.*;

publicclass ShortestPathFinder

{

staticchar grid[][] ={

{'.','.','.','.','.','.','.','.'},

{'.','.','.','.','.','.','.','.'},

{'.','.','.','.','.','.','.','.'},

{'.','.','X','.','A','.','.','.'},

{'.','.','.','.','A','X','.','.'},

{'.','.','.','.','A','.','.','.'},

{'.','.','.','.','.','.','.','.'},

{'.','.','.','.','.','.','.','.'}

};

staticint sLoc[] =newint[2];

staticint eLoc[] =newint[2];

staticchar block ='A', way ='.';

staticfinalint SPOINT = 1;

staticfinalint EPOINT = 2;

publicstaticvoid main (){

display();

int numShapes = 0;

for(int i = 0; i < grid.length; i++){

for (int j = 0; j < grid[i].length; j++){

if(grid[i][j] =='X'){

numShapes ++;

if ( numShapes == 1){

sLoc[0] = i;

sLoc[1] = j;

//System.out.print(sLoc[0] + " " + sLoc[1]);

}else{

eLoc[0] = i;

eLoc[1] = j;

//System.out.print(loc[2] + " " + loc[3]);

}

}

}

}

int x = sLoc[0], y = sLoc[1];

shortPath (grid, x, y);

System.out.println();

//display();

}

// display the whole grid

publicstaticvoid display(){

for(int i = 0; i < grid.length; i++){

for (int j = 0; j < grid[i].length; j++){

System.out.print(grid[i][j] +" ");

}

System.out.println();

}

}

publicstaticvoid shortPath (char g[][],int r,int c){

g [r][c] = SPOINT;

g [eLoc[0]][eLoc[1]] = EPOINT;

if (r < 7 && !(g[r + 1][c] == EPOINT) && !(g[r + 1][c] == block)){// check downward

shortPath(g, r + 1, c);

}

if (r > 0 && !(g[r - 1][c] == EPOINT) && !(g[r - 1][c] == block)){// check upward

shortPath(g, r - 1, c);

}

if (c < 7 && !(g[r][c + 1] == EPOINT) && !(g[r][c + 1] == block)){// check right

shortPath(g, r, c + 1);

}

if (c > 0 && !(g[r][c - 1] == EPOINT) && !(g[r][c - 1] == block)){// check left

shortPath(g, r, c - 1);

}

}

The error is java.lang.StackOverflowError

I don't know how to fix it?

[7724 byte] By [salubadshaa] at [2007-10-2 4:46:50]
# 1
First, don't use recursion if you're getting a stack overflow error.However, if you're getting a stack overflow error, it probably means that you're stuck in some kind of infinite loop.Improve your algorithm.
FuzzyOniona at 2007-7-16 0:51:38 > top of Java-index,Java Essentials,Java Programming...
# 2

I've made few changes in my recursion algorithm but it's still stuck in some kind of infinte loop, i don't understand the reason becuase all the step are there. Why can't it find the final position? Please someone help me please?

if ((r < 7 && !(g[r + 1][c] == g [eLoc[0]][eLoc[1]])) || (g[r + 1][c] == block)) { // check downward

shortPath(g, r + 1, c);

}

if (r > 0 && !(g[r - 1][c] == g [eLoc[0]][eLoc[1]]) || g[r - 1][c] == block) { // check upward

shortPath(g, r - 1, c);

}

if (c < 7 && !(g[r][c + 1] == g [eLoc[0]][eLoc[1]]) || g[r][c + 1] == block) { // check right

shortPath(g, r, c + 1);

}

if (c > 0 && !(g[r][c - 1] == g [eLoc[0]][eLoc[1]]) || g[r][c - 1] == block) { // check left

shortPath(g, r, c - 1);

}

salubadshaa at 2007-7-16 0:51:38 > top of Java-index,Java Essentials,Java Programming...
# 3
when recursion is done properly, the stopping condition is easy to see in the code.i don't see your stopping condition.that, and the StackOverflowError, lead me to believe that your recursion lacks a proper stopping condition.%
duffymoa at 2007-7-16 0:51:38 > top of Java-index,Java Essentials,Java Programming...