javascript - Stop app from crashing when submitting order with empty EditText -
i have started learn programming , writing coffee ordering app. have "price" field enter basic price cup of coffee , other options. right if click "order" button when price field empty app crashes. tried add line of code question possible have both default value , hint in edittext. here bit of code of method calculates price, check boxes check boxes toppings on coffee , function well:
// calculates price private int calculateprice(int quantity) { // price per cup in field edittext pricepercupf = (edittext) findviewbyid(r.id.basic_price); boolean whippedchecked = ((checkbox) findviewbyid(r.id.whipped_cream)).ischecked(); boolean chocolatechecked = ((checkbox) findviewbyid(r.id.chocolate)).ischecked(); int pricepercup = integer.parseint(pricepercupf.gettext().tostring()); if (whippedchecked) { pricepercup += 1; } if (chocolatechecked) { pricepercup += 1; } if (pricepercupf.gettext().tostring().equals("")) { return 0; } else { int price = pricepercup * quantity; return price; }
thanks!
put
if (pricepercupf.gettext().tostring().equals("")) { return 0; }
before
int pricepercup = integer.parseint(pricepercupf.gettext().tostring());
this way exit function before parsing / casting error can appear if field empty.
Comments
Post a Comment