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)
Related
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 1 year ago.
Improve this question
I'm trying to loop over a HashMap called products by the following code :
public Product findProduct (int id)
{
Product result = null;
for (Product product : products.keySet())
{
if (product.getId() == id)
result = product;
break;
}
return result;
}
the thing is at runtime , products contains 2 elements
but the product:products.keySet() returns only the first element and I don't know why, resulting that if I passed 102 to the method, it will throw an exception.
Are you saying that products.keySet() is returning only one element based on the iterations that your loop is going through?
I can see that you've put a break; statement after the result=product; statement which is causing your loop to exit on the first iteration itself. If this wasn't intentional, this might be the culprit here.
You can try to put the statements after if in a block like this
if(product.getId()==id){
result=product;
break;
}
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
}
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 6 years ago.
Improve this question
I have this sample from my project and I need to know why the result is what it is.
public class Main
{
public static void main(String[] args)
{
//url: https://classicpartyrentals.com/products/24681-gothic-silver-coffee-cup, websiteList: http://classicpartyrentals.com/, URL Contains Returns bool: false
String url = "https://classicpartyrentals.com/products/24681-gothic-silver-coffee-cup";
String contains = "http://classicpartyrentals.com/";
System.out.println("Returns bool: " + url.contains(contains));
}
}
Output:
Returns bool: false
Code is always doing what you ask it to do:
String url = "https://classicpartyrentals.com/products/24681-gothic-
but
String contains = "http://classicpartyrentals.com/";
https versus http!
So the real answer is: especially when you are a beginner, chances that your code uncovered "some Java bug" is relatively small (very close to zero in reality!)
There is a much higher chance that your assumptions are wrong. You either don't fully understand the methods you are calling, or there is a subtle flaw in your input data.
Finally: also work on your naming. contains isn't a very good name here; you better call it expectedUrl, or something alike!
In your Code Url "https://classicpartyrentals.com/products/24681-gothic-silver-coffee-cup" contains https
but your compare string "http://classicpartyrentals.com/" contain http so its not match and return false
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
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.
Improve this question
I couldn't figure out why my code is printing forever. I tried a block (will show where I had it originally) but I want it to continue after printing the "help". My code goes as follows. I think it's the for loop... but I haven't touched much on it.
boolean yes = true;
while(yes) {
if(str1.equalsIgnoreCase("Exit")) {
System.exit(0);
} else if (str1.equalsIgnoreCase("Help")) {
for(int g = 0; g < 2; g++) {
System.out.println(" Accepted commands:\n exit\n help\n load [filename]\n students\n search [partial name]\n assignments\n grades\n student [student name]\n assignment [assignment name]\n");
}
//I added the break here and it did print out once but I did not want it to end the program. With the break I did not need the for statement.
} else if (str1.equalsIgnoreCase("Load")) {
}
The value of str1 is never changed inside the loop. Perhaps you are missing some statement that takes new input from the user.
First off
boolean yes = true;
while(yes) {
You are never setting yes to false
Secondly
if(str1.equalsIgnoreCase("Exit")) {
System.exit(0);
str1 is never set inside the loop, even to "Exit"
yes is set to true and you never changed it in the loop
read str1 inside the loop:
DataInputStream in = new DataInputStream(System.in);
str1=in.readLine();
while(true) {
str1=in.readLine();
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 9 years ago.
Improve this question
I'm writing a simple game that uses a loop while(game = true).
I set game = false in the code during the loop. When I run the game, it stops at that point but does not terminate.
However, if I use break instead at the same spot the program terminates. Why can't I use game = false?
This expression
game = true
assigns true togame and the result of this expression is always true.
What you intended as to test if the previous value of game was true
while(game == true)
or much simpler
while(game)
Usually you give it a better name like
while(running) {
at the end of the while loop use
while (game = true)
*whatever happens when the game = true*
break();
if that doesn't work try something like this
while game = (true);
while (game == true) {
*whatever happens in game loop*
}
make sure somewhere before you start your game loop, you have something telling the code when to make game = false. If you don't the game will run forever without ever changing back to false.