Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assign variable in Java while-loop conditional?

I have a method that does a lot of checking and computation and returns a custom class, returnMessage. returnMessage has 2 booleans and a String. What I want to do is run this method in a while loop from my main class and have access to the returnMessage object after the while loop terminates for the last time.

In php this would be a case of

while ( $returned = myObject->myMethod()->finished )
{

}

if( $returned -> finished == FALSE)
{
...
}

However trying to assign like this gives me a boolean expected error in java (there might be php errors in the above, it's late :D )

like image 719
LiamRyan Avatar asked Apr 21 '12 23:04

LiamRyan


People also ask

Can you assign a variable in a while loop?

Yes. you can declare a variable inside any loop(includes do while loop.

Can we assign value in if condition Java?

Yes, you can assign the value of variable inside if.

Can you declare a variable in a for loop Java?

Often the variable that controls a for loop is needed only for the purposes of the loop and is not used elsewhere. When this is the case, it is possible to declare the variable inside the initialization portion of the for.

Is while a conditional statement in Java?

The while and do... while loops in Java are used to execute a block of code as long as a specific condition is met. These loops are similar to conditional if statements, which are blocks of code that only execute if a specific condition evaluates to true.


3 Answers

We need to see more of your code, but guessing a little I think something like this would work:

ReturnMessage returned;
while (!(returned = myObject.myMethod()).finished) {

}
if (!returned.finished) {

}
like image 130
Óscar López Avatar answered Oct 10 '22 06:10

Óscar López


A while loop checks a conditional. Here's another example that can come handy:

public class test {
    public static void main(String[] args) {
        String line;
        while((line = readFromFile())!=null){
//do something with var 'line'
            System.out.println(line);
            break;
        }
    }

    static String readFromFile(){
       return "test string";
    }
}
like image 8
SparkZeus Avatar answered Oct 10 '22 07:10

SparkZeus


While acceptable in PHP, I guess (I don't code in PHP), it is considered extremely bad form to do an assignment within a conditional in Java. (This is because it is error prone and very hard to spot, the difference being between = and == in the middle of a lot of code. This was a deliberate choice based on years of experience with C and C++.)

As it is, I can't quite follow your code. You say "returnMessage has 2 booleans and a String" but the test, as I understand it in PHP, is myObject->myMethod()->finished != null and returned gets set to the value of finished, but then you go and test $returned -> finished which is the same as myObject->myMethod()->finished->finished. Sorry if I misunderstand PHP syntax.

The general recommendation in Java would be more along the lines of:

ReturnMessage returned = myObject.myMethod().getFinished();
while (returned != null) { 

    ...

    returned = myObject.myMethod().getFinished(); // or did you mean myObject.myMethod();   
}

if (!returned.finished) { // or, better: if (!returned.isFinished())
    ...
}

If I misunderstood the PHP, let me know and I'll fix the Java code to match.

like image 3
Old Pro Avatar answered Oct 10 '22 07:10

Old Pro