Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

for loop issues(java)

  • 31-10-2003 11:51am
    #1
    Registered Users, Registered Users 2 Posts: 7,581 ✭✭✭


    i have a for loop running, guarded by the length of a vector (lets call it A ) i'm using inside the loop, the operation I'm running inside is producing a vector for each member of A.

    If something goes wrong during the running and I end up with a smaller vector than i want I discard it, and put the member of A I was working with to the back of A, to be checked after everything else has been.

    Whats happening is the for loop is running, results of my method being returned, if they're fewer than reqrd being checked at the end. Fine, except that that the loop keeps running, passing empty values back to the method. I would set up a check for a null value, except, yest after 4 empty values one that needed to be rechecked was, after which it kept going with null values.

    I presume this could go on infinitely. is the problem is down to changing the value of the guard on the fly?? What can i do about it?

    Thank You, E


Comments

  • Registered Users, Registered Users 2 Posts: 4,666 ✭✭✭Imposter


    I'm not fully sure what you're saying but:

    Are you saying the the method the loop is in is called recursively from the loop?
    If so then the condition for calling this method inside the loop seems to be the problem.
    If that's not what you're saying then maybe post some code and it might be easier to debug.


  • Registered Users, Registered Users 2 Posts: 7,581 ✭✭✭uberwolf


    I'm querying search engines, I pass a query list create a vector from it.

    Inside the loop i want 300 results for each query, if i attempt a search and don't get 300 results I put it to the back to try again. No recursion. The vector starts at size 50, but gets extended if there are problems(i.e fewer than 300 results) and a search term gets thrown to the back...


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Think about it. Lets say you have 10 items, and 3 of them are bad : 2, 4, and 6.

    So, by the time you get through processing the 10 items (0 thru 9), you are only on item 7, as 2, 4, and 6 have been moved to the end.

    You then process what was originally item 2 for the second time. It fails again...you put it to the back. Lets say that items 4 and 6 work 2nd time round.

    So now you're at the last element in the array, which has failed twice. Yuo try it again. It fails again. You put it to the end. You move to the next item...which is the one you just did...and continue failing in an infinite loop unless it ever actually works.

    I'd suggest that what you most likely need is some sort of a "retries" counter, to make sure that no item is attempted more than X times, so that a continuous failure doesn't dump you in a loop.

    jc


  • Registered Users, Registered Users 2 Posts: 7,581 ✭✭✭uberwolf


    thing is they all end up working, i have 300 results for my 50 queries, but it keeps checking for empty queries, its nearly like when its finished going through the vector no longer knows what size it is.

    and i'm reluctant to put a query is null check because yest. I saw it check 4 null queries before finding one it should check,
    thanks E


  • Registered Users, Registered Users 2 Posts: 15,443 ✭✭✭✭bonkey


    Sounds like a logic-bomb then...

    I'd suggest that your best bet is to post up the relevant pieces of the loop - code or simplify to pseudo-code.

    jc


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    I am a bit confused, but shouldn't you not be altering the vector your are using for the for/next loop?


  • Registered Users, Registered Users 2 Posts: 7,581 ✭✭✭uberwolf




  • Registered Users, Registered Users 2 Posts: 1,931 ✭✭✭Zab


    Make sure there is nothing in the queries vector (prior to the for loop) that will never be successful, such as blank strings or nulls.

    Also, generally you shouldn't add another reference to the query to the end of the vector, as this means that your vector will expand indefinitely as long as a query doesn't satisfy your requirements. You should probably remove the successful queries from the vector, and then reiterate at the end to rerun the unsuccessful ones. Iterator is a good class for this.

    Zab.


  • Registered Users, Registered Users 2 Posts: 7,581 ✭✭✭uberwolf


    thanks Zab, I haven't had a chance to implement that yet, (other issues arose) but that makes sense allright.
    Appreciate it,
    E


Advertisement