java string cannot be converted to boolean error -
this question has answer here:
- how compare strings in java? 23 answers
i'm beginner java , can't seem figure out why string won't convert boolean.
the error code comes on "if" line , "else if" line.
string name, systemofmeasure; double height, idealweight; // retrieve user data name = txtname.gettext(); systemofmeasure = txtsystemofmeasure.gettext(); height = double.parsedouble(txtheight.gettext()); if (systemofmeasure = "m") { idealweight = height * height * 25; lbldisplay.settext(name + ("'s ideal weight is") + idealweight); } else if (systemofmeasure = "i") { idealweight = height * height * 25 / 703; lbldisplay.settext(name + ("'s ideal weight is") + idealweight); } else { lbldisplay.settext("please enter m or i"); }
you assigning value m systemofmeasure, if or else if conditions expect boolean expression, not assignment.
so, need use .equals() method (which returns true/false) string comparisons systemofmeasure.equals("m") shown below:
if (systemofmeasure.equals("m")) { //add code } else if (systemofmeasure.equals("i") { //add code } else { //add code } also, remember that, need follow java naming standards lower case variable names systemofmeasure
Comments
Post a Comment