vendredi 16 mars 2018

Java - Condition is always True/False (Android Checkboxes)

I have the following code to display a certain string according to which checkboxes are checked in my Android app:

public String createOrderSummary(){

    CheckBox whippedCheckBox = findViewById(R.id.whipped);
    boolean whippedCream = whippedCheckBox.isChecked();

    CheckBox chocoBox = findViewById(R.id.chocolate);
    boolean chocolate = chocoBox.isChecked();

    if (whippedCream && chocolate){
        return "both selected";
    }else if(whippedCream || chocolate){
        if (whippedCream){
            return "whippedcream";
        }else if (chocolate){
            return "chocolate";
        }
    }else{
        return "none checked";
    }
    return "";
}

I receive a warning at line 14 saying condition chocolate is always true, why is that?

Also when I switch lines to be:

 if (whippedCream){
        return "whipped cream";
    }else if(whippedCream && chocolate){
        return "both selected";
    }else{
        return "none selected";
    }

I receive warning at line 3 saying condition always false.




Aucun commentaire:

Enregistrer un commentaire