Results 1 to 10 of 10
Thread: Vectors Problem
- 01-03-2010, 04:21 PM #1
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
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.
Java 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, 04:42 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
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, 04:55 PM #3
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
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, 04:58 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
- 01-03-2010, 05:03 PM #5
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
Is that in some kind of flag, to be honest I'm a little confussed there :confused:
Dash
- 01-03-2010, 05:37 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,429
- Blog Entries
- 7
- Rep Power
- 17
- 01-03-2010, 05:48 PM #7
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
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, 06:56 PM #8
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
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, 01:53 PM #9
Member
- Join Date
- Dec 2009
- Posts
- 9
- Rep Power
- 0
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-04-2010, 11:16 PM #10
Senior Member
- Join Date
- Aug 2008
- Posts
- 384
- Rep Power
- 5
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.
Whenever you check that object for a match, you set that flag to true.Java Code:boolean visited = false;
Than, in the loop, you should check if that flag is already set to true. If it is, simply ignore the object.Java Code:object.visited = true;
I die a little on the inside...
Every time I get shot.
Similar Threads
-
Sorting JTable (Vectors) Problem
By ramapple in forum AWT / SwingReplies: 6Last Post: 07-06-2009, 11:15 PM -
Vectors of Vectors or hash-somethings?
By mindwarp in forum New To JavaReplies: 3Last Post: 03-10-2008, 02:57 PM -
Vectors vs ArrayList
By Java Tip in forum Java TipReplies: 0Last Post: 11-28-2007, 10:29 AM -
problem with Vectors and getTotal() function
By java_fun2007 in forum New To JavaReplies: 2Last Post: 11-23-2007, 01:55 PM -
Problem with vectors in java
By toby in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks