Assign variable in Java while-loop conditional?
Asked Answered
C

4

24

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 )

Choroiditis answered 21/4, 2012 at 23:48 Comment(1)
The difference between PHP and Java here is that while in Java requires a boolean-typed expression (someVar = boolExpr is itself a boolean-typed expression). If the variable assigned was a bool then it would be identical. Please show the current Java code as it is likely that something of non-bool is being assigned. In any case, with few exceptions, I would recommend not using this idiom...Bryce
T
53

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) {

}
Testimonial answered 21/4, 2012 at 23:54 Comment(0)
T
8

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";
    }
}
Teth answered 8/7, 2018 at 2:55 Comment(2)
Does this actually compile? I'm using Java 7 and the syntax is incorrect. line must be defined before the loop.Sputter
@Sputter Thanks for pointing this out. 'line' should be defined before the loop but can be initialised inside the while condition.Teth
J
3

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.

Jocko answered 22/4, 2012 at 5:25 Comment(1)
this would never enter the while loop.Meredith
I
1

this is possible like so

while( (returned=myObject.myMethod()).isFinished()){

}

or to be more precise (returned=myObject.myMethod()) assigns the object returned to returned and you can use it like a normal var

Issus answered 21/4, 2012 at 23:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.