java - Errors when EditText left blank -


can take @ below code , point out obvious on what's wrong? program gives error when edittext left blank , calculation once information present. program crashes when there text missing, why this?

edittext edittext1, edittext2; double numa, numb, numc; textview answer;  @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.calc);      edittext1 = (edittext) findviewbyid(r.id.edittext1);     edittext2 = (edittext) findviewbyid(r.id.edittext2); }  public void btnclick(view v) {     if(edittext1.gettext().length()==0)     {         edittext1.seterror("please input text");      }{         if  (edittext2.gettext().length()==0)             {                 edittext2.seterror("please input text");             }      numa = new double(edittext1.gettext().tostring());     numb = new double(edittext2.gettext().tostring());      numc=(numa + numb); answer.settext(double.tostring(numc));     } }} 

the problem you're trying parse "" number (a double, specifically). checking empty values, you're still attempting parse if set error. need place new double() statements in try-catch block, , catch exceptions happen when trying parse invalid input. way, it's safe if user inputs random string of text not parseable double.


Comments

Popular posts from this blog

jquery - Invalid Assignment Left-Hand Side -

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -