• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

break and resume at the same point in a for loop

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks -

a beginners question in Java - I have two nested for loops - the inner loop has a condition which causes a break out of the inner for loop if a condition is satisfied - Once resumed I am guessing the for loop will begin at the beginning - how do I make it resume at the point after it break here is the flow

String myStrings (an array of strings)  stored in this class -


// function hwich takes in a list of strings and compares the contents to myStrings above
bool checkOrder (String... stringInput) {

 for (String given : stringInput) {
           boolean found = false;
           for (String stored : myStrings) {
               if (stored.equals(given)) {
                   found = true;
// so here I have to break out  - but when I am back in this for loop would like to resume at the point where I broke out and NOT at the beginning -
                  break;
               }
           }
}
}


}

Thanks a million

Farrukh
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, that sounds like you don't actually want to break out of the loop.
 
Farrukh Azfar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi thanks for your answer - I believe I do - because I am chekcing to see if the strings appear in a particular sequence - so the stored list and the given list should both for example posses "apple" "pear" "dates" in that order ... for which the nested loops above are neccesary
 
Rancher
Posts: 5008
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to start the loops at the point where they left off in previous executions, use a for loop with indexes that can be saved and used again instead of foreach loops.
 
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't you determine the intersection first,before going on? For instance, list.retainAll,  and then process each In the intersection?
 
Farrukh Azfar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,
thanks but there is no currentIndex() method in the String given or String given variables - I would use the index but I can't see how to get to it - of course I can declare an integer and increment it instead - but I don't think that is what you were suggesting -

Farrukh
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Still don't see the need to break.

Indeed, it doesn't look like nested loops to me.

So, some pseudo code:

Start at index 0.
for each String in myStrings.
   If String matches the index for stringInput
       increment index
       if checked all stringInput then break with true
 
Farrukh Azfar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

thanks again - I have not presented the whole code which is why its hard to say this is needed - I appreciate your suggestion and this is exactly what I had wanted to do but there seems to be no way to use an integer index in this for loop

ie.

index i = 0;
for (String myString : manyStrings) {

// there is no way to use
System.out.println( " Index " manyStrings.currentIndex(i).toString());

// In other words there is no way to access the array manyStrings(i)  -
}

So it doesnt work - what does one use ?

Farrukh
 
Dave Tolls
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The index is used against stringInput, not manyStrings.

stringInput is an array, so stringInput[index].

 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This kind of practice is a maintenance/debugging nightmare and is precisely why Structured Programming was developed as a discipline. It leads to "spaghetti code".

A block should ideally have either entry point(s) or exit point(s) in the middle, but NOT both. And for absolute best results, there should only be one entry or exit so as to eliminate confusion.

Jumping into loops is especially problematic.

Mathematical analysis indicates that all possible programming logic can be done using pure structured programming elements (and by pure, I mean without any goto or label capabilities at all). So I would recommend looking more closely at what you are trying to do.
 
Farrukh Azfar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Million thanks Dave, I am a complete beginner at Java - your patience is appreciated -
Farrukh

Dave Tolls wrote:The index is used against stringInput, not manyStrings.

stringInput is an array, so stringInput[index].

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic