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.
java for-loop while-loop break
add a comment |
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.
java for-loop while-loop break
3
Whywhile(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
add a comment |
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.
java for-loop while-loop break
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
java for-loop while-loop break
edited Nov 18 at 14:09
asked Oct 29 at 17:33
LearningToJava
7801823
7801823
3
Whywhile(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
add a comment |
3
Whywhile(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
add a comment |
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
}
add a comment |
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;
}
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, ifisUpright()
can transition fromfalse
totrue
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
add a comment |
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 ++;
}
}
}
add a comment |
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;
}
add a comment |
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
Without more detailed specification, ifisUpright()
can transition fromfalse
totrue
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
add a comment |
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;
}
add a comment |
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
}
add a comment |
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
}
add a comment |
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
}
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
}
answered Oct 29 at 17:49
Andreas
73.3k456118
73.3k456118
add a comment |
add a comment |
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;
}
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, ifisUpright()
can transition fromfalse
totrue
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
add a comment |
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;
}
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, ifisUpright()
can transition fromfalse
totrue
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
add a comment |
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;
}
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;
}
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, ifisUpright()
can transition fromfalse
totrue
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
add a comment |
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, ifisUpright()
can transition fromfalse
totrue
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
add a comment |
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 ++;
}
}
}
add a comment |
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 ++;
}
}
}
add a comment |
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 ++;
}
}
}
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 ++;
}
}
}
answered Oct 29 at 17:47
Alain Cruz
1,5211818
1,5211818
add a comment |
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
answered Oct 29 at 17:49
Andrew S
1,4721510
1,4721510
add a comment |
add a comment |
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
Without more detailed specification, ifisUpright()
can transition fromfalse
totrue
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
add a comment |
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
Without more detailed specification, ifisUpright()
can transition fromfalse
totrue
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
add a comment |
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
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
answered Oct 29 at 17:39
Nicholas K
4,04031029
4,04031029
Without more detailed specification, ifisUpright()
can transition fromfalse
totrue
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
add a comment |
Without more detailed specification, ifisUpright()
can transition fromfalse
totrue
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
add a comment |
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;
}
add a comment |
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;
}
add a comment |
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;
}
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;
}
answered Oct 29 at 18:31
vinayak
11
11
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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