# Vectors Problem

• 01-03-2010, 05:21 PM
dashwall
Vectors Problem
Hi having a problem with vectors and looping through them, what I have happening is the program goes into a massive continues loop.

The idea behind it is it should first check for matches if matches found it should move on in the loop to next object.

If no matches found it should then check to see if it has found its destination and move on in the loop.

If neither two objects have been found its should then move on in the loop adding but it just gets caught in a never ending loop.

any help or suggestions the code is below.
Code:

```private void routePlanner()   {       Vector route = new Vector();       int layover = 0;       int totalTime = 0;       String startRoute = portList();       String endRoute = portList();       String result = "";       String visited1="";       String visited2="";       String visited3="";       String visited4="";       String visited5="";       String visited6="";       String visited7="";       String visited8="";       String visited9="";       for (int row = 0; row < oceanLink.length; row++)//initial loop through array.       {         if (oceanLink[row][1].matches(startRoute))         {             String code = oceanLink[row][0];             String startPort = oceanLink[row][1];             String finishPort = oceanLink[row][2];             double departureTime = Double.parseDouble(oceanLink[row][3]);             int startTime = calculationDepart(departureTime);             double arrivalTime = Double.parseDouble(oceanLink[row][4]);             int finishTime = calculationArrive(arrivalTime);             int layOver = layover;             totalTime = startTime - finishTime;             RouteInitalise temp = new RouteInitalise(code, startPort, finishPort, startTime, finishTime, layOver, totalTime,visited1,visited2,visited3,visited4,               visited5,visited6,visited7,visited8,visited9);             route.add(temp);         }       }           for(int counter=0; counter<route.size();counter++)//loop for vector         {             RouteInitalise tmp =(RouteInitalise)route.elementAt(counter);             if(tmp.Destinationport.matches(tmp.Portname)||tmp.Destinationport.matches(tmp.Visited1)||tmp.Destinationport.matches(tmp.Visited2)||tmp.Destinationport.matches(tmp.Visited3)               ||tmp.Destinationport.matches(tmp.Visited4)||tmp.Destinationport.matches(tmp.Visited5)||tmp.Destinationport.matches(tmp.Visited6)||tmp.Destinationport.matches(tmp.Visited7)               ||tmp.Destinationport.matches(tmp.Visited8)||tmp.Destinationport.matches(tmp.Visited9))//check for matches             {               System.out.println("Match Found");                           }             if (tmp.Destinationport.matches(endRoute))//check to see if hit destination points             {               int times=Integer.parseInt(tmp.Layover);               double wait=convert(times);               times=Integer.parseInt(tmp.SailTime);               double sailTime=convert(times);               times=(Integer.parseInt(tmp.Layover)+Integer.parseInt(tmp.SailTime));               double totalTravel=convert(times);               result=result+"To go from "+startRoute+" to "+endRoute+"\n"+"you would need to go Via "+tmp.Visited1+"\n"+tmp.Visited2+"\n"+tmp.Visited3+"\n"+tmp.Visited4+"\n"+tmp.Visited5+"\n"+tmp.Visited6+"\n"+tmp.Visited7+"\n"+tmp.Visited8+"\n"+tmp.Visited9+"\n"                   +"Giving a Total Layover time of "+numform.format(wait)+"\n"+"Also a total sailing time of "+numform.format(sailTime)+"\n"+"Giving you a total travel time of "+numform.format(totalTravel);               System.out.println(result);             }               for(int row1=0; row1<oceanLink.length; row1++)// loop through array               {                 if (oceanLink[row1][1].matches(tmp.Destinationport))// check for matches to destination port                 {                   String code=tmp.SailingCode;                   String startPort=tmp.Portname;                   String finishPort=oceanLink[row1][2];                   double departureTime=Double.parseDouble(oceanLink[row1][3]);                   int startTime=calculationDepart(departureTime);                   double arrivalTime=Double.parseDouble(oceanLink[row1][4]);                   int finishTime=calculationArrive(arrivalTime);                   int layOver=Integer.parseInt(tmp.ArrivalTime);                   layOver=layOver+finishTime;                   totalTime=Integer.parseInt(tmp.SailTime);                   totalTime=totalTime + (startTime - finishTime);                   visited1=tmp.Destinationport;                   visited2=tmp.Visited1;                   visited3=tmp.Visited2;                   visited4=tmp.Visited3;                   visited5=tmp.Visited4;                   visited6=tmp.Visited5;                   visited7=tmp.Visited6;                   visited8=tmp.Visited7;                   visited9=tmp.Visited8;                   RouteInitalise temp = new RouteInitalise(code, startPort, finishPort, startTime, finishTime, layOver, totalTime,visited1,visited2,visited3,visited4,                   visited5,visited6,visited7,visited8,visited9);                   route.add(temp);                   //add all to vector                           }               }         }       JOptionPane.showMessageDialog(null,result);   }```
• 01-03-2010, 05:42 PM
JosAH
Print your 'counter' and 'row1' values as well as the size of your Vector each time through your loop; you'll find what's wrong then.

kind regards,

Jos
• 01-03-2010, 05:55 PM
dashwall
Thanks for that suggestion Jos made intresting reading.

Form what I could see the loops are working well but what the program is doing is readding vectors to places it has already visited.

I think I would need to move through the loop deleting areas that match and -1 from the counter.

Would that work and how would I code that because its bouncing between pre visited ports.

Dash
• 01-03-2010, 05:58 PM
JosAH
Quote:

Originally Posted by dashwall
Thanks for that suggestion Jos made intresting reading.

Form what I could see the loops are working well but what the program is doing is readding vectors to places it has already visited.

I think I would need to move through the loop deleting areas that match and -1 from the counter.

Would that work and how would I code that because its bouncing between pre visited ports.

Dash

For each place you can add a field 'visited' and set it to true once you have visited that place; you have to anticipate for that in your loop(s).

kind regards,

Jos
• 01-03-2010, 06:03 PM
dashwall
Is that in some kind of flag, to be honest I'm a little confussed there :confused:

Dash
• 01-03-2010, 06:37 PM
JosAH
Quote:

Originally Posted by dashwall
Is that in some kind of flag, to be honest I'm a little confussed there :confused:

Dash

Yep, I was suggesting a flag per location; when that location is visited set the flag to true and when you want to check it again in your loops (it needn't be checked if its flag happens to be set to true).

kind regards,

Jos
• 01-03-2010, 06:48 PM
dashwall
so I would code that like this.

In my route Intialise object I did set up 3 booleans as visited, StartPort and EndPort all set to false.

but not fully sure how to use them tbh. :(

Dash
• 01-03-2010, 07:56 PM
dashwall
Ok I think I get the logic beind the flags just struggling how to initalise them and where and how to compensate for them in the for loop.

Thanks
Dash
• 01-04-2010, 02:53 PM
dashwall
Ok so been looking at my code for awhile now, But still nothing seems to be jumping out at me about how I can get round the problem.

I have seven days to get this done and its stressing me out. Any suggestions pointing me in the right direction would be appericiated.

Dash
• 01-05-2010, 12:16 AM
Supamagier
Your code is almost as ugly as Visual Basic code. You should have a look at the Java code conventions. ;)

What josah meant: Give each object a boolean variable, called visible, or any name you like.
Code:

`boolean visited = false;`
Whenever you check that object for a match, you set that flag to true.
Code:

`object.visited = true;`
Than, in the loop, you should check if that flag is already set to true. If it is, simply ignore the object.