Loop though foreach a set number of times

Hi Guys
I want to know how I can loop though a foreach loop a set number of times ? I know in a for loop you can set the max number of loops but I don't know how to do it when comes to a foreach loop. Below is the structure of my foreach loop:
for(Letter letter : letters) {
  //I want to loop though only 50 objects max at a time
}

List<Letter> prefix = (letters.size() <= limit) ? letters : letters.subList(0, limit);
for(Letter letter : prefix) {
}

Similar Messages

  • How to loop an swf a set number of times?

    I need to loop a banner animation a set number of times, then
    get it to gotoandstop on a final frame. I know in the past I could
    use:
    counter++; if (counter<5) { gotoAndPlay (1); }
    At least for the looping part (don't even know yet about the
    stop pon a particular frame part).
    but now in Flash 8 I can't get my test swf to do that using
    this statement. Am I missing something? (Of course, obviously I am
    or I wouldn't be here!)

    This works for me as well, but i don't want it to jump to the
    final still frame. i need mine to loop three times, then plan
    through to the frame i want, stopping there. the frame i want is
    not the final frame in the swf. it is somewhere in the middle.
    does that make sense?

  • Running a java program a set number of times

    This is a general question. Is it possible to make a java program run only 5 times for the sake of arguement.
    Basically I want to write a program that will give the user some flexibility when it will actually run another Java program, but I only want them to be able to say "not now' for a set number of times. When the last time comes the other program will launch. I was initially thinking of the Do Whilw loop, but this needs to work when the program is restarted.
    Program starts, it has 5 times it will run before it does something else(doesn't really matter now I think). User takes option "Not Now" and the program ends, but warns the user this will run 4 more times before you will need to do something.
    This process will repeat until the user takes the option "Ok install now" or the time limit expires and the install occurs anyway. Can someone point me in the right direction.

    ok I see so it's like one those programs that you download for free on the internet and they give you a set amount times to use it before you have to pay for it. but in this case when the number of times you use it equals 5 (or when the user clicks ok) a different java app will open automatically.
    My first thought would be to Write a Serialized object to disk using objectOutputStream that stores the number of times the application has been opened. and each time the program runs it checks for the serialized object and then you can do something like what I posted before. of course if were worried about security the user could always look for the object and erase it, if so then I guess we would have to come up with another plan of attack
    Hope this helps

  • Using skip logic to show sections of the form a set number of times

    Hi
    I have form which i need all to fill out basic information and a section which asks for furtehr information if a certain condition is true. what i'm looking ofr is this section of the form to be shown again as long as a condition is true or for x numbe rof times.
    the form collects basic information about event companies and information about one event, i have a question which asks howmany events per year do you run. if this is more than one or set to true section part of the form would need to be shown again for x number of times to collect the information about thos events also.
    If not is there any workaround i could implemnet using skip logic to acheieve an efective solution?
    thanks

    Yeah that is the work around. This post explains how:
    http://forums.adobe.com/thread/1142978?tstart=0
    Its a little tedious to setup up but it should work.
    Randy

  • Loop control for set number of times per loop

    Hello everyone,
    I am having a problem with an assignment, I have gotten it all to work except one thing. The assignment was to create a quiz asking for the planets in order by using JOptionPane input dialog,loop, and StringBuffer. If the user answers incorrect show a message dialog with a hint and ask the same question again. If the user gets it wrong again, then just move on to the next question. I got it to work except I cant figure out how to make it ask the question again only once if the answer is wrong. Here is what I have so far:
       import javax.swing.*;
        public class SolarSystemQuiz {
           public static void main(String[] args) {
             String answer;
             int i ;
             int count = 0;
             int correct = 0;
             int wrong = 0;
             boolean crt;
             for (i=1; i < 10; ++i) {
                StringBuffer str = new StringBuffer("What is the " + getPlace(i) + " planet in the solar system?");
                do {
                   answer = JOptionPane.showInputDialog(null, str);
                   crt = getAnswer(i, answer);
                   if (crt) {
                      ++correct;
                      count = 0;
                   else{
                      ++count;
                      ++wrong;
                } while ( ! crt && count <= 1);
             JOptionPane.showMessageDialog(null, "You got " + correct +
                " answers correct\n, and " + wrong + " incorrect answers.");
             System.out.println(correct + wrong);
             System.exit(0);
           public static String getPlace(int spot) {
             switch (spot) {
                case 1:
                   return "1st";
                case 2:
                   return "2nd";
                case 3:
                   return "3rd";
                case 4:
                   return "4th";
                case 5:
                   return "5th";
                case 6:
                   return "6th";
                case 7:
                   return "7th";
                case 8:
                   return "8th";
                case 9:
                   return "9th";
                default:
                   return "You have created an error, please try again.";
           public static boolean getAnswer(int planet, String guess ) {
             String mercury = "mercury" ;
             String venus = "venus";
             String earth = "earth";
             String mars = "mars" ;
             String jupiter = "jupiter";
             String saturn = "saturn";
             String uranus = "uranus";
             String neptune = "neptune";
             String pluto = "pluto" ;
             switch (planet) {
                case 1:
                   if (guess.equalsIgnoreCase("Mercury")) {
                      JOptionPane.showMessageDialog(null, "Correct!");
                      return true;  
                   else {
                      JOptionPane.showMessageDialog(null,
                         "not it, the planet starts with M and has " + mercury.length() + " letters");
                      return false;
                case 2:
                   if (guess.equalsIgnoreCase("Venus")) {
                      JOptionPane.showMessageDialog(null, "Correct!");
                      return true;
                   else {
                      JOptionPane.showMessageDialog(null,
                         "not it, the planet starts with " + venus.charAt(0) + " and has " + venus.length() + " letters");
                      return false;
                case 3:
                   if (guess.equalsIgnoreCase("Earth")) {
                      JOptionPane.showMessageDialog(null, "Correct!");
                      return true;
                   else {
                      JOptionPane.showMessageDialog(null,
                         "not it, the planet starts with " + earth.charAt(0) + " and has " + earth.length() + " letters");
                      return false;
                case 4:
                   if (guess.equalsIgnoreCase("Mars")) {
                      JOptionPane.showMessageDialog(null, "Correct!");
                      return true;
                   else {
                      JOptionPane.showMessageDialog(null,
                         "not it, the planet starts with " + mars.charAt(0) + " and has " + mars.length() + " letters");
                      return false;
                case 5:
                   if (guess.equalsIgnoreCase("Jupiter")) {
                      JOptionPane.showMessageDialog(null, "Correct!");
                      return true;
                   else {
                      JOptionPane.showMessageDialog(null,
                         "not it, the planet starts with " + jupiter.charAt(0) + " and has " + jupiter.length() + " letters");
                      return false;
                case 6:
                   if (guess.equalsIgnoreCase("Saturn")) {
                      JOptionPane.showMessageDialog(null, "Correct!");
                      return true;
                   else {
                      JOptionPane.showMessageDialog(null,
                         "not it, the planet starts with " + saturn.charAt(0) + " and has " + saturn.length() + " letters");
                      return false;
                case 7:
                   if (guess.equalsIgnoreCase("Uranus")) {
                      JOptionPane.showMessageDialog(null, "Correct!");
                      return true;
                   else {
                      JOptionPane.showMessageDialog(null,
                         "not it, the planet starts with " + uranus.charAt(0) + " and has " + uranus.length() + " letters");
                      return false;
                case 8:
                   if (guess.equalsIgnoreCase("Neptune")) {
                      JOptionPane.showMessageDialog(null, "Correct!");
                      return true;
                   else {
                      JOptionPane.showMessageDialog(null,
                         "not it, the planet starts with " + neptune.charAt(0) + " and has " + neptune.length() + " letters");
                      return false;
                case 9:
                   if (guess.equalsIgnoreCase("Pluto")) {
                      JOptionPane.showMessageDialog(null, "Correct!");
                      return true;
                   else {
                      JOptionPane.showMessageDialog(null,
                         "not it, the planet starts with " + pluto.charAt(0) + " and has " + pluto.length() + " letters");
                      return false;
                default:
                   JOptionPane.showMessageDialog(null, "Not in this universe, try again");
                   return false;
       } Thanks for your time,
    Brad

    This was a perfect example of how fast and easy a fix can be when the OP provides sufficient information and code.

  • Loop in motion assistant specified number of times?

    I was wondering if in motion assistant I can do a loop in my script so that each movement can be repeated many, many times without having to create a new 'step' for each movement? Any help is appreciated.
    Thanks,
    Nichole

    Nichole,
    Motion Assistant does not currently support loops in its environment. What I recommend is that you create your steps with just one pass and then add the loop in the code Motion Assistant generates.
    Rodger S.

  • Looping a movie clip a specific number of times in Flash

    Does anyone know how to get a movie clip to loop a specific number of times in Flash? I know how to stop a movie clip from looping by using the this.stop (); command by placing the command in a separate Action Script layer, in a keyframe, inside of the movie clip's timeline. This allows the movie clip to play through once and then stop. But I need for the movie clip to loop more than once, maybe 2 or 3 times, and then go back to the main timeline. Does anyone know the code for this?
    Also, is it possible to place a pause (I'm guessing, maybe by using a timer of some type?) between the loops, so that the movie will pause a couple of seconds before it loops again and then stop? Please note I do not need the movie clip to stop when there's an event like a rollover or anything. I just need it to play a couple of times, pause between plays and then stop and go back to the main timeline. Please let me know if anyone can help.
    Thanks,
    Sarah
    P.S. Is there a good, easy to use, reference book anyone can recommend for creating specific things in Flash using Action Script? Do you guys have a favorite for beginners like me?

    You can use a variable to keep count of how many times you loop, and until it exceeds a particulr value (2 or 3) you execute a setTimeout() call that waits for however long you want to delay and then tells the movieclip to gotoAndPlay(1).
    So in the first frame you would establish the counting variable...
    var count;
    if(count == undefined){ // only set it to 0 once
    count = 0;
    and in the last frame you would do your incrementing and control....
    stop();
    count += 1;
    if(count < 3){
    setTimeout(waitABit, 2000); // call function in 2 secs
    function waitABit(){
    gotoAndPlay(1);

  • Adding a counter that keeps track of the total number of times a loop has run, even if LabVIEW has been restarted.

    Hi all,
    I am writing a VI for measuring data and inserting it into a database. The measurements are controlled by a loop that runs once every minute. I want to give each measurement a unique id number and store this too in the database.
    To do this, I want to add a counter to this loop so that I can count the number of times the loop has executed in total. This is, even if the VI, LabVIEW or even th PC is restarted, I want the counter to keep track of the number of executions. If say, the loope executes two times and then the VI is stopped and restarted, I want the following number on the counter to be three.
    Does anyone have an idea about how to do this? I am gratefule for any help!
    Clara
    Message Edited by Clara G on 05-11-2010 08:21 AM
    Solved!
    Go to Solution.

    Not allowed to give away code but I can describe one of my "Totalizers" used to keep track of how much stuff has passed through a fliter so we know when to change it.
    THe Total izer is implemented as an Action Engine.
    It has three actions (methods)
    1) Init - Opens an ini file and reads the last values read and cahces these in shift registers. It also inits a timer so we now how long since the last file I/O.
    2) Update - Uses the data passed by the caller to update the totals. It also checks how long since the last save and if more than one minute it writes to the ini file (forced write).
    3) Read - returns the totals for display and evealuating if a an alarm should be triggered to change the filter.
    Note:
    THe pre-LV 8.6 version of the ini file exposed methods to allow writing to the file. The new ini functions do not expose that functionality and require closing the file.
    Ben
    Ben Rayner
    I am currently active on.. MainStream Preppers
    Rayner's Ridge is under construction

  • When using my new nano 7 with nike , my music stops after the 30 minute voice alert even though I have set my run for over 30 minutes.It cancels/deletes my run/workout when the music stops.This has happened several times.How can I keep this from happening

    When using my new nano 7 with nike+, my music stops after the 30 minute voice alert even though I have set my run/workout for over 30 minutes.  It also cancels/deletes that run/workout.  This has happened several times.  How can I keep this from happening?

    If possible I would arrange for a good service center to go over it. Hopefully they have a variety of parts in stock. They can test and verify all is working. The previous attempts are based on guesses by the person at the other end of the phone without diagnostic tests to assess the culprit. It is extremely unlikely for a webcam to fail & for it & a Mic to fail together should be an indicator to a good technician it is something else. EG Motherboard
    Take it to a servicing dealer and wait for it.
    T520 Model 4239 Intel(R) Core(TM) i7-2860QM CPU @ 2.50GHz
    Intel Sandy Bridge & Nvidia NVS 4200M graphics Intel N 6300 Wi-Fi adapter
    Windows 7 Home Prem - 64bit w/8GB DDR3

  • Number of times an animation loops

    I have an animation looping in Edge using a label and a trigger. How can I specify the number of times the animation loops? Currently it cycles endlessly.

    Darrell,
    Thanks for your patience. Here's a couple a screen captures in my Edge project. It stil loops endlessly. Any thoughts on what I'm missing?

  • My iphone time keeps messing up even though its on "set automatically" and i dont know how to fix it

    my iphone 5cs' time keeps messing up even though its set to "set automatically" the time doesnt change and i have no idea how to fix it. any help please?

    I'm having the EXACT same problem! I have a great wifi connection and it all started right after I updated it.
    I took it to the AT&amp;T store and all they said that they could do for it was a "soft restart" which is where you hold down the lock button and the home button UNTIL you see the apple logo. And I did this like 4 times and it still messes up. When I turn it off and turn it on its fine for a few minutes but it slowly starts messing up again.
    And I'm also having several glitches. Like when I just received a text message when my phone was locked I slid it and put in my pass code then it like turned off and turned on my phone. And sometimes the messages that I send disappear from my side even though I sent them. If I turn it off then back on it's there. And also sometimes when I text a person and hit send it like backs me out of the messaging system(it still sends the message right then)  and back to the home screen.
    I called 1-800-myiphone and the lady did a "soft restart" which they call a "hard restart" and it worked for like 35 minutes and then it messed up again. And my iCloud isn't backing up my stuff so it's not like I can just completely reset my phone.

  • I made a 46.5 min aperture slide show and tried a number of times to Export it so I can burn it to DVD to be viewed on an HDTV, so far with no success!  I set computer sleep to 3hrs and let it export over night and the furthest i've gotten is 66% Exported

    I made a 46.5 min aperture slide show and tried a number of times to Export it so I can burn it to DVD to be viewed on an HDTV, so far with no success!  I set computer sleep to 3hrs and let it export over night and the furthest i've gotten is 66% Exported! Why is it taken so long and seem to stop but the activity status goes up ever so slowley???? Im using an iMac  7,1   2.4 GHz with 6GB of memory running 10.8.2 what am I doing wrong???

    Rendering will take a very long time  - I once needed 6 hours for a shorter high definition movie with plenty of effects.
    What theme are you using and what settings?
    The "places" theme needs a good network connection.
    And the finished movie will need plenty of disk space.
    And the finished movie will need plenty of disk space. Have you checked the available  space on your disk?
    What exactly happens when Aperture fails to render the movie? Are there any diagnostics, error messages in the console window, that relate to Aperture? Does Aperture alway stop at the same image or video clip?
    if all fails, you could render your slideshow in two parts and combine the two videos on your DVD.
    Regards
    Léonie

  • Execute case structure a certain number of times within a while loop

    Hi guys,
    I was wondering if it was possible to execute a case structure a specified number of times within a while loop. Basically I'm running a camera where each iteration of the outer while loop displays a frame of a live video feed and I'd like to capture the next x amount of frames (specified by the user) each time the 'Start Capture' button is clicked. The block diagram within the case structure captures and saves an image frame. As the program is written right now, frames will continue to be captured until the 'Start Capture' button is clicked again. Instead of having to click the button again to stop the case structure from executing for each iteration of the while loop, I'd like it to only execute for the next x iterations of the while loop. Any help would be appreciated. Thanks.

    Hello Uxorious,
    A while loop has a iteration counter that gives an integer value of how many times the loop has iterated. You can utilise this to count the number of frames captured. I assume that the program could be running for any amount of time before clicking start capture, if that is the case then you will need to be able to detect when you have clicked the button and add the desired number of frames you wish to capture to the current loop iteration.
    If you use a shift register on the outer loop with the start capture button connected to it (inside the loop) you will be able to compare its current value to its previous value. If it changes from false to true then you can add the desired number of frames to it to give you the loop iteration value where you need to stop.
    Then you can compare the current loop iteration value to that number and once reached, you can use a property node to change the value of your start capture button back to false.
    One final thing, don't forget to initialise the shift register I suggested to false so that you don't run the risk of missing the first time you press the button.
    I hope that makes sense.
    Alistair
    Best regards,
    Ali Bailey
    National Instruments
    Don't forget to Kudos useful posts!

  • Continuing a loop for a set amount of time

    Hi,
    I'm am trying to alter a previously written program have run into a problem.
    The program is set up so that when the input exceeds a set level an output voltage to a buzzer is created; the buzzer continues as long as there is an input.  What I need it to do is to continue this output for a set amount of time.
    I've been searching the forum and found this straight forward solution from adamsjr.
    "The easiest way to do this is to use a two Tick Count functions. The first
    one is outside of a While loop, the second one is inside of the While loop.
    Wire the first one through a tunnel into the While loop, add on the appropriate
    ms (5*1000), wire the sum to one input of a compare function. Wire the
    other input of the compare function to the second Tick Count function. Exit
    the loop when the compare shows the second Tick count value is greater than
    the summed value."
    I've got placed the output subVI into a while loop and followed the above directions and this doesn't seem to work. I'm wondering if I would also have to include the input subVI into the loop or is there another possible solution
    Thanks ahead of time,
    Kristen

    Hi Kristen,
    You have the compare function implemented incorrectly.  You need to make sure that the first terminal of the "Greater than or equal" function connected to the output of the sum, and the second terminal to the tick count in the while loop.  The first terminal corresponds to x parameter and the second terminal is for the y parameter.  So if x is greater than or equal to y, then the loop will need to exit.
    You can see how long a subVI is taking if you put a flat sequence structure within your subVI around the entire subVI code.  The first frame would only have a tick count going into an indicator, the middle frame your normal code, and then the final frame would contain another tick count wired into another indicator.  Then you can subtract the second indicator value from the first and get hold long it took to execute the middle (core) of your code for that subVI.
    I highly suggest using the highlight execution and probes and other debugging techniques such as breakpoints to diagnose where the data is being held up.

  • Choose a set number of files in a folder?

    Is there a way using AppleScript to choose a set number of files inside a folder?
    I've created an Automator workflow to convert some files (Illustrator to PDF) but the folders contain a large number of files (nearly 500). If I ran the workflow on this amount of files I'm pretty sure it would crash, as the workflow opens the files & then saves them in the other format (so it would open all 500 files).
    So what the workflow does at the moment is ask me to choose the files to convert, I select around 50 & let it convert them & closes the files. It loops, so once it's completed them I it asks again to choose more files to convert etc...
    If I could make it choose a set number of files (via some AppleScript & add this to the workflow) it could open & convert (for example) 50 files, then open & convert the next files. And keep doing this until it completes converting all of them.

    red_menace wrote:
    What happens to the original item after the conversion? Is it moved, or will some method need to be used to keep track of what has already been converted?/Trim Input Items.action.zip
    After the files have been converted they are moved to another folder so that I don't choose them again & again, & so that I know they've been converted.
    So if I used the Trim Input Items action it would choose only 9 items, convert them & then (with the workflow looping) convert another 9 files, & so on & so on. Is that correct? If so that could work for me.
    The Dispense Items Incrementally action could also work for me. If I understand correctly it would convert 1 file at a time & I'd set to loop so it goes through all the items 1 at a time.
    Camelot wrote:
    I don't see the need to select a set number of files in the folder.
    From your description it sounds like you want to process them all anyway, it's just that your existing (manual-select) workflow works by selecting a few at a time.
    What's wrong with the script identifying all files and processing them all in one loop?
    Because my workflow (at the moment) opens all the files at once, & then converts them. So it would open nearly 500 Illustrator files in one go rather then opening a few of them, or opening them 1 at a time. I'm pretty sure it would choke my system opening up 500 files (not particularly small files either).

Maybe you are looking for