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











up vote
0
down vote

favorite












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.










share|improve this question




















  • 3




    Why while(true)?
    – ernest_k
    Oct 29 at 17:38










  • @ernest_k Sorry, I'm quite new to Java. What's wrong with doing so?
    – LearningToJava
    Oct 29 at 17:41










  • FYI: OP has said: "It's more than possible for the bot to fall over again."
    – Andreas
    Oct 29 at 18:00















up vote
0
down vote

favorite












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.










share|improve this question




















  • 3




    Why while(true)?
    – ernest_k
    Oct 29 at 17:38










  • @ernest_k Sorry, I'm quite new to Java. What's wrong with doing so?
    – LearningToJava
    Oct 29 at 17:41










  • FYI: OP has said: "It's more than possible for the bot to fall over again."
    – Andreas
    Oct 29 at 18:00













up vote
0
down vote

favorite









up vote
0
down vote

favorite











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.










share|improve this question















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.







java for-loop while-loop break






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 18 at 14:09

























asked Oct 29 at 17:33









LearningToJava

7801823




7801823








  • 3




    Why while(true)?
    – ernest_k
    Oct 29 at 17:38










  • @ernest_k Sorry, I'm quite new to Java. What's wrong with doing so?
    – LearningToJava
    Oct 29 at 17:41










  • FYI: OP has said: "It's more than possible for the bot to fall over again."
    – Andreas
    Oct 29 at 18:00














  • 3




    Why while(true)?
    – ernest_k
    Oct 29 at 17:38










  • @ernest_k Sorry, I'm quite new to Java. What's wrong with doing so?
    – LearningToJava
    Oct 29 at 17:41










  • FYI: OP has said: "It's more than possible for the bot to fall over again."
    – Andreas
    Oct 29 at 18:00








3




3




Why while(true)?
– ernest_k
Oct 29 at 17:38




Why while(true)?
– ernest_k
Oct 29 at 17:38












@ernest_k Sorry, I'm quite new to Java. What's wrong with doing so?
– LearningToJava
Oct 29 at 17:41




@ernest_k Sorry, I'm quite new to Java. What's wrong with doing so?
– LearningToJava
Oct 29 at 17:41












FYI: OP has said: "It's more than possible for the bot to fall over again."
– Andreas
Oct 29 at 18:00




FYI: OP has said: "It's more than possible for the bot to fall over again."
– Andreas
Oct 29 at 18:00












6 Answers
6






active

oldest

votes

















up vote
1
down vote



accepted










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
}





share|improve this answer




























    up vote
    1
    down vote













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





    share|improve this answer



















    • 2




      You can get rid of the outer while loop
      – Dane White
      Oct 29 at 17:38










    • @DaneWhite Correct. I was already working on an edit. Thanks for pointing that out
      – GBlodgett
      Oct 29 at 17:39






    • 2




      Without more detailed specification, if isUpright() can transition from false to true over time, it is feasible that it can transition the other way too, so by the time you get to the last bot, the first bot may have fallen again. Not getting into the issues of concurrency, you should probably restart the check after the wait.
      – Andreas
      Oct 29 at 17:44








    • 1




      @Andreas Indeed, that's right. It's more than possible for the bot to fall over again.
      – LearningToJava
      Oct 29 at 17:47








    • 1




      @GBlodgett Thanks. Exactly what I was looking for. That works for now! :)
      – LearningToJava
      Oct 29 at 17:49


















    up vote
    1
    down vote













    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 ++;
    }
    }
    }





    share|improve this answer




























      up vote
      1
      down vote













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





      share|improve this answer




























        up vote
        0
        down vote













        Remove the outer while and use it inside the for-loop instead of the if



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


        There is no need for the outer while-loop






        share|improve this answer





















        • Without more detailed specification, if isUpright() can transition from false to true over time, it is feasible that it can transition the other way too, so by the time you get to the last bot, the first bot may have fallen again. Not getting into the issues of concurrency, you should probably restart the check after the wait.
          – Andreas
          Oct 29 at 17:44










        • Makes sense, valid question. I guess the as per the question the bots don't fall once they are up!.
          – Nicholas K
          Oct 29 at 17:50










        • Actually, OP has said "It's more than possible for the bot to fall over again" in a comment.
          – Andreas
          Oct 29 at 17:53






        • 1




          Oh didn't notice that. Then he's clearly marked the wrong answer as expected. BTW he should have also mentioned that in the question rather than a comment on someone else's answer.
          – Nicholas K
          Oct 29 at 17:54


















        up vote
        0
        down vote













        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;

        }





        share|improve this answer





















          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53050909%2fbreak-while-loop-only-when-all-conditions-in-for-loop-are-met%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          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
          }





          share|improve this answer

























            up vote
            1
            down vote



            accepted










            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
            }





            share|improve this answer























              up vote
              1
              down vote



              accepted







              up vote
              1
              down vote



              accepted






              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
              }





              share|improve this answer












              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
              }






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Oct 29 at 17:49









              Andreas

              73.3k456118




              73.3k456118
























                  up vote
                  1
                  down vote













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





                  share|improve this answer



















                  • 2




                    You can get rid of the outer while loop
                    – Dane White
                    Oct 29 at 17:38










                  • @DaneWhite Correct. I was already working on an edit. Thanks for pointing that out
                    – GBlodgett
                    Oct 29 at 17:39






                  • 2




                    Without more detailed specification, if isUpright() can transition from false to true over time, it is feasible that it can transition the other way too, so by the time you get to the last bot, the first bot may have fallen again. Not getting into the issues of concurrency, you should probably restart the check after the wait.
                    – Andreas
                    Oct 29 at 17:44








                  • 1




                    @Andreas Indeed, that's right. It's more than possible for the bot to fall over again.
                    – LearningToJava
                    Oct 29 at 17:47








                  • 1




                    @GBlodgett Thanks. Exactly what I was looking for. That works for now! :)
                    – LearningToJava
                    Oct 29 at 17:49















                  up vote
                  1
                  down vote













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





                  share|improve this answer



















                  • 2




                    You can get rid of the outer while loop
                    – Dane White
                    Oct 29 at 17:38










                  • @DaneWhite Correct. I was already working on an edit. Thanks for pointing that out
                    – GBlodgett
                    Oct 29 at 17:39






                  • 2




                    Without more detailed specification, if isUpright() can transition from false to true over time, it is feasible that it can transition the other way too, so by the time you get to the last bot, the first bot may have fallen again. Not getting into the issues of concurrency, you should probably restart the check after the wait.
                    – Andreas
                    Oct 29 at 17:44








                  • 1




                    @Andreas Indeed, that's right. It's more than possible for the bot to fall over again.
                    – LearningToJava
                    Oct 29 at 17:47








                  • 1




                    @GBlodgett Thanks. Exactly what I was looking for. That works for now! :)
                    – LearningToJava
                    Oct 29 at 17:49













                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









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





                  share|improve this answer














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






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Oct 29 at 17:39

























                  answered Oct 29 at 17:37









                  GBlodgett

                  7,14441329




                  7,14441329








                  • 2




                    You can get rid of the outer while loop
                    – Dane White
                    Oct 29 at 17:38










                  • @DaneWhite Correct. I was already working on an edit. Thanks for pointing that out
                    – GBlodgett
                    Oct 29 at 17:39






                  • 2




                    Without more detailed specification, if isUpright() can transition from false to true over time, it is feasible that it can transition the other way too, so by the time you get to the last bot, the first bot may have fallen again. Not getting into the issues of concurrency, you should probably restart the check after the wait.
                    – Andreas
                    Oct 29 at 17:44








                  • 1




                    @Andreas Indeed, that's right. It's more than possible for the bot to fall over again.
                    – LearningToJava
                    Oct 29 at 17:47








                  • 1




                    @GBlodgett Thanks. Exactly what I was looking for. That works for now! :)
                    – LearningToJava
                    Oct 29 at 17:49














                  • 2




                    You can get rid of the outer while loop
                    – Dane White
                    Oct 29 at 17:38










                  • @DaneWhite Correct. I was already working on an edit. Thanks for pointing that out
                    – GBlodgett
                    Oct 29 at 17:39






                  • 2




                    Without more detailed specification, if isUpright() can transition from false to true over time, it is feasible that it can transition the other way too, so by the time you get to the last bot, the first bot may have fallen again. Not getting into the issues of concurrency, you should probably restart the check after the wait.
                    – Andreas
                    Oct 29 at 17:44








                  • 1




                    @Andreas Indeed, that's right. It's more than possible for the bot to fall over again.
                    – LearningToJava
                    Oct 29 at 17:47








                  • 1




                    @GBlodgett Thanks. Exactly what I was looking for. That works for now! :)
                    – LearningToJava
                    Oct 29 at 17:49








                  2




                  2




                  You can get rid of the outer while loop
                  – Dane White
                  Oct 29 at 17:38




                  You can get rid of the outer while loop
                  – Dane White
                  Oct 29 at 17:38












                  @DaneWhite Correct. I was already working on an edit. Thanks for pointing that out
                  – GBlodgett
                  Oct 29 at 17:39




                  @DaneWhite Correct. I was already working on an edit. Thanks for pointing that out
                  – GBlodgett
                  Oct 29 at 17:39




                  2




                  2




                  Without more detailed specification, if isUpright() can transition from false to true over time, it is feasible that it can transition the other way too, so by the time you get to the last bot, the first bot may have fallen again. Not getting into the issues of concurrency, you should probably restart the check after the wait.
                  – Andreas
                  Oct 29 at 17:44






                  Without more detailed specification, if isUpright() can transition from false to true over time, it is feasible that it can transition the other way too, so by the time you get to the last bot, the first bot may have fallen again. Not getting into the issues of concurrency, you should probably restart the check after the wait.
                  – Andreas
                  Oct 29 at 17:44






                  1




                  1




                  @Andreas Indeed, that's right. It's more than possible for the bot to fall over again.
                  – LearningToJava
                  Oct 29 at 17:47






                  @Andreas Indeed, that's right. It's more than possible for the bot to fall over again.
                  – LearningToJava
                  Oct 29 at 17:47






                  1




                  1




                  @GBlodgett Thanks. Exactly what I was looking for. That works for now! :)
                  – LearningToJava
                  Oct 29 at 17:49




                  @GBlodgett Thanks. Exactly what I was looking for. That works for now! :)
                  – LearningToJava
                  Oct 29 at 17:49










                  up vote
                  1
                  down vote













                  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 ++;
                  }
                  }
                  }





                  share|improve this answer

























                    up vote
                    1
                    down vote













                    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 ++;
                    }
                    }
                    }





                    share|improve this answer























                      up vote
                      1
                      down vote










                      up vote
                      1
                      down vote









                      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 ++;
                      }
                      }
                      }





                      share|improve this answer












                      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 ++;
                      }
                      }
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Oct 29 at 17:47









                      Alain Cruz

                      1,5211818




                      1,5211818






















                          up vote
                          1
                          down vote













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





                          share|improve this answer

























                            up vote
                            1
                            down vote













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





                            share|improve this answer























                              up vote
                              1
                              down vote










                              up vote
                              1
                              down vote









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





                              share|improve this answer












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






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Oct 29 at 17:49









                              Andrew S

                              1,4721510




                              1,4721510






















                                  up vote
                                  0
                                  down vote













                                  Remove the outer while and use it inside the for-loop instead of the if



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


                                  There is no need for the outer while-loop






                                  share|improve this answer





















                                  • Without more detailed specification, if isUpright() can transition from false to true over time, it is feasible that it can transition the other way too, so by the time you get to the last bot, the first bot may have fallen again. Not getting into the issues of concurrency, you should probably restart the check after the wait.
                                    – Andreas
                                    Oct 29 at 17:44










                                  • Makes sense, valid question. I guess the as per the question the bots don't fall once they are up!.
                                    – Nicholas K
                                    Oct 29 at 17:50










                                  • Actually, OP has said "It's more than possible for the bot to fall over again" in a comment.
                                    – Andreas
                                    Oct 29 at 17:53






                                  • 1




                                    Oh didn't notice that. Then he's clearly marked the wrong answer as expected. BTW he should have also mentioned that in the question rather than a comment on someone else's answer.
                                    – Nicholas K
                                    Oct 29 at 17:54















                                  up vote
                                  0
                                  down vote













                                  Remove the outer while and use it inside the for-loop instead of the if



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


                                  There is no need for the outer while-loop






                                  share|improve this answer





















                                  • Without more detailed specification, if isUpright() can transition from false to true over time, it is feasible that it can transition the other way too, so by the time you get to the last bot, the first bot may have fallen again. Not getting into the issues of concurrency, you should probably restart the check after the wait.
                                    – Andreas
                                    Oct 29 at 17:44










                                  • Makes sense, valid question. I guess the as per the question the bots don't fall once they are up!.
                                    – Nicholas K
                                    Oct 29 at 17:50










                                  • Actually, OP has said "It's more than possible for the bot to fall over again" in a comment.
                                    – Andreas
                                    Oct 29 at 17:53






                                  • 1




                                    Oh didn't notice that. Then he's clearly marked the wrong answer as expected. BTW he should have also mentioned that in the question rather than a comment on someone else's answer.
                                    – Nicholas K
                                    Oct 29 at 17:54













                                  up vote
                                  0
                                  down vote










                                  up vote
                                  0
                                  down vote









                                  Remove the outer while and use it inside the for-loop instead of the if



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


                                  There is no need for the outer while-loop






                                  share|improve this answer












                                  Remove the outer while and use it inside the for-loop instead of the if



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


                                  There is no need for the outer while-loop







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Oct 29 at 17:39









                                  Nicholas K

                                  4,04031029




                                  4,04031029












                                  • Without more detailed specification, if isUpright() can transition from false to true over time, it is feasible that it can transition the other way too, so by the time you get to the last bot, the first bot may have fallen again. Not getting into the issues of concurrency, you should probably restart the check after the wait.
                                    – Andreas
                                    Oct 29 at 17:44










                                  • Makes sense, valid question. I guess the as per the question the bots don't fall once they are up!.
                                    – Nicholas K
                                    Oct 29 at 17:50










                                  • Actually, OP has said "It's more than possible for the bot to fall over again" in a comment.
                                    – Andreas
                                    Oct 29 at 17:53






                                  • 1




                                    Oh didn't notice that. Then he's clearly marked the wrong answer as expected. BTW he should have also mentioned that in the question rather than a comment on someone else's answer.
                                    – Nicholas K
                                    Oct 29 at 17:54


















                                  • Without more detailed specification, if isUpright() can transition from false to true over time, it is feasible that it can transition the other way too, so by the time you get to the last bot, the first bot may have fallen again. Not getting into the issues of concurrency, you should probably restart the check after the wait.
                                    – Andreas
                                    Oct 29 at 17:44










                                  • Makes sense, valid question. I guess the as per the question the bots don't fall once they are up!.
                                    – Nicholas K
                                    Oct 29 at 17:50










                                  • Actually, OP has said "It's more than possible for the bot to fall over again" in a comment.
                                    – Andreas
                                    Oct 29 at 17:53






                                  • 1




                                    Oh didn't notice that. Then he's clearly marked the wrong answer as expected. BTW he should have also mentioned that in the question rather than a comment on someone else's answer.
                                    – Nicholas K
                                    Oct 29 at 17:54
















                                  Without more detailed specification, if isUpright() can transition from false to true over time, it is feasible that it can transition the other way too, so by the time you get to the last bot, the first bot may have fallen again. Not getting into the issues of concurrency, you should probably restart the check after the wait.
                                  – Andreas
                                  Oct 29 at 17:44




                                  Without more detailed specification, if isUpright() can transition from false to true over time, it is feasible that it can transition the other way too, so by the time you get to the last bot, the first bot may have fallen again. Not getting into the issues of concurrency, you should probably restart the check after the wait.
                                  – Andreas
                                  Oct 29 at 17:44












                                  Makes sense, valid question. I guess the as per the question the bots don't fall once they are up!.
                                  – Nicholas K
                                  Oct 29 at 17:50




                                  Makes sense, valid question. I guess the as per the question the bots don't fall once they are up!.
                                  – Nicholas K
                                  Oct 29 at 17:50












                                  Actually, OP has said "It's more than possible for the bot to fall over again" in a comment.
                                  – Andreas
                                  Oct 29 at 17:53




                                  Actually, OP has said "It's more than possible for the bot to fall over again" in a comment.
                                  – Andreas
                                  Oct 29 at 17:53




                                  1




                                  1




                                  Oh didn't notice that. Then he's clearly marked the wrong answer as expected. BTW he should have also mentioned that in the question rather than a comment on someone else's answer.
                                  – Nicholas K
                                  Oct 29 at 17:54




                                  Oh didn't notice that. Then he's clearly marked the wrong answer as expected. BTW he should have also mentioned that in the question rather than a comment on someone else's answer.
                                  – Nicholas K
                                  Oct 29 at 17:54










                                  up vote
                                  0
                                  down vote













                                  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;

                                  }





                                  share|improve this answer

























                                    up vote
                                    0
                                    down vote













                                    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;

                                    }





                                    share|improve this answer























                                      up vote
                                      0
                                      down vote










                                      up vote
                                      0
                                      down vote









                                      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;

                                      }





                                      share|improve this answer












                                      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;

                                      }






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Oct 29 at 18:31









                                      vinayak

                                      11




                                      11






























                                           

                                          draft saved


                                          draft discarded



















































                                           


                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function () {
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53050909%2fbreak-while-loop-only-when-all-conditions-in-for-loop-are-met%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Costa Masnaga

                                          Fotorealismo

                                          Sidney Franklin