mardi 11 juin 2019

Where the first IF condition is true, code statements in the ELSE section are still processed [duplicate]

This question already has an answer here:

As my code is processing inside the first IF statement, for some reason it still proceeds to execute the other statements inside my ELSE statement.

Thoughts?

(Note: I welcome code optimization suggestions also)

public class Parse {

    String price = parsePrice("FREE");

    public static String parsePrice(String price) {
        if (price == "FREE") {
            return "0.00";
        } else {
            price = price.replaceAll(',','');
            return price.replaceAll('$','');
        }
    }

}

Expected Results: After the IF code processes, it breaks out of the IF/Else code block This is the only code that should execute:

return "0.00"; 

Actual Results: After the IF code processes, it proceeds to process the Else code This is the code that executes:

return "0.00";
price = price.replaceAll(',','');
return price.replaceAll('$','');

Aucun commentaire:

Enregistrer un commentaire