Enigmatic
Enigmatic

Reputation: 4148

Break while loop only when all conditions in for loop are met

I would like to break a while loop only when all of the "bots" are upright. (*These bots being referred to are mini USB robots).

.upRight() returns true when a bot is standing and false when not.

public static boolean checkSomething() throws ... {

        while (true) {

            for (i = 0; i < bots; i++) { // bots = 2
                if (!theMainBots[i].isUpright()) {
                    ...
                    Thread.sleep(1000); 
                } 
                else {
                    return true;

                }
            }
        }

The issue I'm facing, is that if the isUpright() method returns true for the first "bot", then all other bots are left unchecked and may return false. The intention is to wait for the user to place the bot in an upright position before proceeding.

Upvotes: 0

Views: 107

Answers (5)

vinayak
vinayak

Reputation: 1

you can create a list from array of bots , iterate over this list using iterator if a particular bot is upright , remove it from this list using iterator.remove. outer while will run until list is not empty.

public static boolean checkUpright()  {
    ArrayList<Bot> notUprightBots=   (ArrayList<Bot>) Arrays.asList(theBots);
    while (!notUprightBots.isEmpty()) {
        Iterator<Bot> iterator=notUprightBots.iterator();
        while(iterator.hasNext()){
            Bot bot=iterator.next();
            if (!bot.isUpright()) {
                System.out.println("Please ensure I'm upright");
                try{
                     Thread.sleep(500); 
                 }catch(InterruptedException  e){
                 } 

            }else {
                iterator.remove();
            }
        }
    }
    return true;

}

Upvotes: 0

Andreas
Andreas

Reputation: 159086

You should check all the bots first, then act on the result. Don't try to act on the result inside the check loop.

Also, since the code doesn't return until all bots are upright, the method is misnamed and shouldn't return a value.

public static void waitUntilAllUpright() throws InterruptedException {
    for (;;) { // forever loop
        boolean allUpright = true;
        for (i = 0; i < bots; i++) {
            if (! theBots[i].isUpright()) {
                allUpright = false;
                break;
            } 
        }
        if (allUpright)
            return;
        System.out.println("Please ensure I'm upright");
        Thread.sleep(500); 
    } // loop back to check all bots again
}

Upvotes: 1

Andrew S
Andrew S

Reputation: 2756

The context is not completely clear, but mixing logical control with user interaction might be the problem. Perhaps this approach might work:

public static boolean checkUpright() throws InterruptedException {
    while (!areAllBotsUpright()) {
        System.out.println("Please ensure I'm upright");
        Thread.sleep(500);
    }
}

public static boolean areAllBotsUpright() {
   for (i = 0; i < bots; i++) {
       if (!theBots[i].isUpright()) {
           return false;     
       } 
   }

   return true;
}

Upvotes: 1

Alain Cruz
Alain Cruz

Reputation: 5097

One way to achieve this, is using a variable that will determine when to leave the loop. Your problem here, is that you also need to change your for loop for a while loop. Why is that? Because you don't know if the bot you just checked was moved or not. Also, the outer loop is unnecessarily, unless you wanted to recheck again. So the code would end looking something like this.

public static boolean checkUpright() throws InterruptedException {

    int counter = 0;
    while (counter <= theBots.length) { // bots = 2
        if (!theBots[i].isUpright()) {
            System.out.println("Please ensure I'm upright");
            Thread.sleep(500); 
        } else {
            counter ++;
        }
    }
}

Upvotes: 1

GBlodgett
GBlodgett

Reputation: 12819

If you want to wait until the user makes the bot upright you could change the if to a while:

while (true) {
     for (i = 0; i < bots; i++) { // bots = 2
         while(!theBots[i].isUpright()) {
             System.out.println("Please ensure I'm upright");
             Thread.sleep(500); 
         } 
     }               
     return true;     
}

This will loop through each element in the Array and while any given bot is not upright, it will loop and sleep until the bot is turned upright. In which case, you do not need to while(true) loop:

public static boolean checkUpright() throws InterruptedException {
      for (i = 0; i < bots; i++) { // bots = 2
         while(!theBots[i].isUpright()) {
             System.out.println("Please ensure I'm upright");
             Thread.sleep(500); 
         } 
     }               
     return true;    
}

Upvotes: 1

Related Questions