简体   繁体   中英

Execute Code Between If and Else-if Statement in Java

I got this code:

if (conditionX) {

                    doX();
                }



else if (conditionY) {
                    boolean isSomethingTrue = findOutIfSomethingIsTrue();
                    if (anotherConditionIsTrue) {
                        doY();
                    }
                }

XYZPojo xyzPOJO = getXYZPOJO();    

else if (xyzPOJO.getValueX().contains("xyz")) {  // <- eclipse wants me to delete this 'else' statement

                    doXYZ();

                }

When I run this code eclipse complains that "Syntax error on token "else", delete this token" . I understand this is because I have the code:

boolean conditionXYZ = checkIfXYZIsTrue();

between both else if statements. But I cannot check the value of conditionXYZ unless I set the value of conditionXYZ in between both else if statements.

The only other similar question I found on SO is this one and it suggests to use two if statements. The problem is I don't want conditionXYZ to be evaluated if conditionY is true.

My other option is to use two if statements and add a return statement for each if statement. But I do not find this solution to be elegant in terms of code design.

My third option is to checkIfXYZIsTrue() before executing any of the if statements but this is not efficient because if conditionY is true we don't need to check conditionXYZ .

Any ideas?

Thanks in advance

Java syntax doesn't allow you to have statements between chains of if/else (that's not in their respective blocks).

You can execute your third condition in the else if condition:

if (conditionX) {
    doX();
} else if (conditionY) {
    boolean isSomethingTrue = findOutIfSomethingIsTrue();
    if (anotherConditionIsTrue) {
        doY();
    }
} else {
    XYZPojo xyzPOJO = getXYZPOJO();
    if (xyzPOJO.getValueX().contains("xyz")) {
        doXYZ();
    }
    //reuse xyzPOJO
}

Alternatively, the last else can still look like:

} else if (getXYZPOJO().getValueX().contains("xyz")){
    doXYZ();
}

I know this is pretty old, but seeing as no one gave the answer I would have, I figured I would post my answer incase anyone else finds their way here looking for options.

The problem is I don't want conditionXYZ to be evaluated if conditionY is true.

You could use a ternary operator to check that conditionY is false before evaluating conditionXYZ above the if/else statements.

//Sets xyzPOJO to getXYZPOJO() if conditionY isn't true, otherwise set xyzPOJO to null
XYZPojo xyzPOJO = conditionY!=true ? getXYZPOJO() : null;

if (conditionX) {
    doX();
}
else if (conditionY) {
    boolean isSomethingTrue = findOutIfSomethingIsTrue();
    if (anotherConditionIsTrue) {
        doY();
    }
}
else if (xyzPOJO.getValueX().contains("xyz")) {
    doXYZ();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM