Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I was wondering if it was possible to check for multiple strings to be true in one while statement. The || operator doesn't work and I can't think of another way to make this possible.
Even though I know the statement bellow isn't possible, I want another statement with the same functionality.
while (!color1.equals("Red")) || (!color2.equals("Green"))
Your requirement is not quite clear to me so I'll give two possible answers.
If at least one condition must be met then you should use || operator (be careful with your parentheses by the way - they should wrap both conditions):
// at least one condition is met
while ((!color1.equals("Red")) || (!color2.equals("Green"))) {
// do something
}
Otherwise, if both conditions must be met, instead of || you need to use && operator.
// both conditions are met
while ((!color1.equals("Red")) && (!color2.equals("Green"))) {
// do something
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
This is only my third program in java. I'm stuck on this part of the problem.
"The formula is not valid if T > 50 in absolute value or if v > 120 or < 3"
I'm not sure how to translate this into code while restricted to use the following:
no, if statements
no, loops
no, importing
no, new classes to be added
Thank you!
public boolean validateFormula(int T, int v) {
return !(abs(T)>50 || v > 120 || v < 3);
}
In giving you a direct answer to this problem, you might not be able to replicate it next time, or to be able to think for yourself.
In import java.util.* , Math.abs(int n) will return the absolute value of n. The || operator means 'or'. I will not assume that you know the basic structure of an if loop so here:
if(condition || condition || condition){
//execute code here
}
You have everything that you need to solve this, good luck. Furthermore, if you do have any more simple questions like this, I would suggest doing a google search instead.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Sorry for, perhaps, obvious question, but i can't add delay into lambda expression. I tried:
Callable<int> task = () -> {TimeUnit.SECONDS.sleep(1); concurrentHashMap.get(treeNum).getApples()};
and IDE shows "Missing return statement".
How to add delay?
Lambda expressions can be written like this p -> inlineMethod(), ie with an implicit return statement only when there is only one statement.
Otherwise, you must have a code block like this p -> { [...]; return null; }. In this case, you must add explicitly the return statement (one or more) into the block.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I'm using a while loop and it won't terminate when it should. If it was working correctly, then it would terminate when randno was either == highbound or == lowbound.
Code of the loop:
do {
do {
randno = (int) (Math.round((Math.random()*(4)) + 0.5)-1);
direction = getDirection(randno,heading);
} while (robot.look(direction)==IRobot.WALL);
System.out.println(randno);
System.out.println(highbound);
System.out.println(lowbound);
System.out.println("---------------");
} while (randno!=lowbound | randno!=highbound);
The output is either 3 3 2 ------, or 2 3 2 ------, so the loop should end. The first loop ends properly (I embedded them to try and get it to work...). What is going wrong?
randno!=lowbound | randno!=highbound is always true, since randno can't be equal to both lowbound and highbound (assuming they are not equal).
Therefore the loop never terminates.
If you wish to terminate when randno is different than both bounds, change your condition to :
while (randno==lowbound || randno==highbound)
If you wish to terminate when randno is the same as one of the bounds, change your condition to :
while (randno!=lowbound && randno!=highbound)
EDIT : based on your question, you want the second option.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The error I got is-
Main.java:23: error: bad operand types for binary operator '||'
if(c=='a'||c=='e'||c=='i'||c='o'||c=='u'||c=='y')
^
first type: boolean
second type: char
I didnt really quite understand it.
The reason is this:
c='o'
in your if condition.
Probably you are just doing this assignment by mistake. So you may want to update this to comparison using
c=='o'
||c='o'||c=='u'
you were using an assignment operator.
||c=='o'||c=='u'
if(c=='a'||c=='e'||c=='i'||c='o'||c=='u'||c=='y')
change c='o' to c=='o'
Actually assignment was happening in your if statement which is not allowed
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
this question might be simple for many of you but im stuck in it:/
this if condition works without while around it, but when i put while i get nothing in log
this REresponse is a response coming from server with a particular id that client sent to it plus a T or F as result to make sure the only client recieves it is the one who sent it
boolean recieved = false
String REresponse = (String) sInput.readObject();
while(recieved=false)
{
if (REresponse.equals("('T','"+newRID+"')")){
Log.i(LOGTAG,"you made it");
recieved = true;
}else if (REresponse.equals("('F','"+newRID+"')")){
Log.i(LOGTAG,"you failed");
recieved=true;
}else{
Log.i(LOGTAG,REresponse);
Log.i(LOGTAG,"some thing is wrong");
also i tried to use case condition but it does accpet this
the condition should be
while (received == false){
//...
}
if you write received = false you make an assignment, the boolean operator is ==
recieved = false. This always results in false so you never go into the loop when
You're assigning rather than equality in condition for while. You need to do as in follows.
while(received == false)