java string cannot be converted to boolean error -


this question has answer here:

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

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -