Results 1 to 3 of 3
  1. #1
    alin_ms is offline Member
    Join Date
    Dec 2008
    Posts
    11
    Rep Power
    0

    Default Weird problem upon calling same function twice

    Hi all this is my 3rd post with the same problem. i am calling a function from a class which should calculate a rounting path. Algorithm works fine it does return the path as i want it when i call it first time. Second time though when i call this function it returns me the same path. I can't understand why. I paste here my class and in red will be the code i found out that the secodn time the algorithm doesn't go into.


    Java Code:
    import java.util.*;
    
    //import PathFinder.Position;
    
    public class PathFinder
    {
    
    public static class Position {
    		private int row;
    		private int col;
    		
    		Position(int trow,int tcol){
    			row = trow;
    			col = tcol;
    		}
    	};
    	
    	public static int[][] map; // map size depends on the tests
    	public static int length;// = 5; //initial map length is 5
    	public static int height;
    	public static int pathLength;
    	//private ArrayQueue q;
    	public Position[] path;
    	public static Position start, finish;
    	public int startx,starty,finishx,finishy;
    	
    	public PathFinder(int maplength, int mapheight, int[][] mapp, int startPx, int startPy, int finishPx,int finishPy){
    		length = maplength;
    		height = mapheight;
    		//map = new int[length+2][height+2];
    		map = mapp;
    		startx = startPx;
    		starty = startPy;
    		finishx = finishPx;
    		finishy = finishPy;
    		setBounds();
    		start = new Position(startx, starty);
    		finish = new Position(finishx, finishy);
    		//q = new ArrayQueue();
    	}
    	
    	public PathFinder(int maplength, int mapheight, int[][] mapp, int agentId, int finishPx, int finishPy){
    		length = maplength;
    		height = mapheight;
    		map = mapp;
    		finishx = finishPx;
    		finishy = finishPy;
    		setBounds();
    	}
    	
    	public void setBounds(){
    		 for (int i=0;i<=length+1;i++){
    				map[0][i] = map[length+1][i] = 1;
    				map[i][0] = map[i][length+1] = 1;
    			}
    	}
    	
    	public static void printMap(){
    		for(int i = 0; i < length+2; i++){
                for(int j = 0; j<length+2; j++)
                    System.out.print(map[i][j] + " ");
                System.out.println();
            }
    	}
    	
    	public boolean findPath(){
    		ArrayQueue q = new ArrayQueue();
    		System.out.println("searching path...");
    		
    		if ((start.row == finish.row)&(start.col == finish.col)){
    			pathLength = 0;
    			return false;
    		}
    		Position[] offset = new Position[4];
    		
    		offset[0] = new Position (0,1);
    		offset[1] = new Position (1,0);
    		offset[2] = new Position (0,-1);
    		offset[3] = new Position (-1,0);
    		
    		Position here = new Position(start.row,start.col);
    		
    		int nrOfNbrs = 4;
    
    		Position nbr = new Position(0,0);
    		[COLOR="Red"]do{
    			for (int i=0;i<nrOfNbrs;i++)
    			{
    	             nbr.row = here.row + offset[i].row;
    	             nbr.col = here.col + offset[i].col;
    		             if (map[nbr.row][nbr.col] == 0)
    		             {  // unlabeled nbr, label it
    		            	 
    		                map[nbr.row][nbr.col] = map[here.row][here.col] + 1;
    		                if ((nbr.row == finish.row) && (nbr.col == finish.col)) 
    		                	break; // done
    		                q.add(new Position(nbr.row,nbr.col));
    		             }
    		             else if(map[nbr.row][nbr.col] == 1){
    		            	 break;
    		             }
    			}
    			
    			 if ((nbr.row == finish.row) && (nbr.col == finish.col)) 
    				 break;
    			 
    			 if (q.isEmpty()) return false;          // no path
    		         here = (Position) q.remove();  // get next position
    
    	      } while(true);[/COLOR]
    
    
    		pathLength = map[finish.row][finish.col] - 2;
            path = new Position [pathLength];
            
            here = finish;
    
            for (int j = pathLength - 1; j >= 0; j--)
            {
               path[j] = here;
               // find predecessor position
               for (int i = 0; i < nrOfNbrs; i++)
               {
                  nbr.row = here.row + offset[i].row;
                  nbr.col = here.col + offset[i].col;
                  	if (map[nbr.row][nbr.col] == j + 2) 
                  		break;
               }
               here = new Position(nbr.row, nbr.col);  // move to predecessor
            }
            return true;
    	}
    	
    	   synchronized public int[][] outputPath()
    	   {System.out.println("Path finder says: pathLength is:" + pathLength);
    		  int[][] result = new int[pathLength][2];
    	      System.out.println("The wire path is");
    	      for (int i = 0; i < pathLength; i++){
    	         //System.out.println("path is:" + path[i].row + " " + path[i].col);
    	      	 //map[path[i].row][path[i].col] = 8;}
    	         result[i][0] = path[i].row;
    	         result[i][1] = path[i].col;
    	      }
    	      return result;
    	   }
    	   /** entry point for wire routing program */
    
    //	   public static void main(String [] args)
    //	   {
    //	      inputData();
    //	      if (findPath()) 
    //	    	  outputPath();
    //	      	else System.out.println("There is no wire path");
    //	      printMap();
    //	   }
    }
    Any help will be gratefull.

    Thank you all

  2. #2
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default hhhmmm...

    An observation and a suggestion:
    • In the following code you're using "&", not "&&". Not sure if that's what you want or if that will help you any with your problem:

    Java Code:
    if ((start.row == finish.row)[B][COLOR="Red"]&[/COLOR][/B](start.col == finish.col)){
    			pathLength = 0;
    			return false;
    		}
    • Have you tried to putting println's in the code to debug and follow the flow of data ?


    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  3. #3
    alin_ms is offline Member
    Join Date
    Dec 2008
    Posts
    11
    Rep Power
    0

    Default

    yes i did println all the way. but still no luck i will try what u suggest .

    Thankx

Similar Threads

  1. problem calling function from class to class
    By alin_ms in forum New To Java
    Replies: 3
    Last Post: 12-19-2008, 07:35 PM
  2. Replies: 9
    Last Post: 09-30-2008, 07:30 AM
  3. Calling Java Function through JSP
    By Vikrant Pawar in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 06-06-2008, 06:28 AM
  4. problem with Vectors and getTotal() function
    By java_fun2007 in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 01:55 PM
  5. Function declaration problem.
    By snooze-g in forum Advanced Java
    Replies: 3
    Last Post: 07-18-2007, 09:15 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
  •