Results 1 to 10 of 10

Thread: Vectors Problem

  1. #1
    dashwall is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default 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);
       }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default

    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

  3. #3
    dashwall is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    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

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by dashwall View Post
    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

  5. #5
    dashwall is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

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

    Dash

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,729
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by dashwall View Post
    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

  7. #7
    dashwall is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    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

  8. #8
    dashwall is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    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

  9. #9
    dashwall is offline Member
    Join Date
    Dec 2009
    Posts
    9
    Rep Power
    0

    Default

    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

  10. #10
    Supamagier is offline Senior Member
    Join Date
    Aug 2008
    Posts
    384
    Rep Power
    7

    Default

    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.
    Java Code:
    boolean visited = false;
    Whenever you check that object for a match, you set that flag to true.
    Java 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.
    I die a little on the inside...
    Every time I get shot.

Similar Threads

  1. Sorting JTable (Vectors) Problem
    By ramapple in forum AWT / Swing
    Replies: 6
    Last Post: 07-07-2009, 12:15 AM
  2. Vectors of Vectors or hash-somethings?
    By mindwarp in forum New To Java
    Replies: 3
    Last Post: 03-10-2008, 03:57 PM
  3. Vectors vs ArrayList
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-28-2007, 11:29 AM
  4. problem with Vectors and getTotal() function
    By java_fun2007 in forum New To Java
    Replies: 2
    Last Post: 11-23-2007, 02:55 PM
  5. Problem with vectors in java
    By toby in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 06:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •