java - 1 error wont let my code compile -
i new android programming , trying make app splits bill between people. integers wrong. know because in logcat says "caused by: java.lang.numberformatexception: invalid int: ""
here mainactivity.java
public class mainactivity extends activity { public double x; public double y; protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //button button btn = (button) findviewbyid(r.id.button); //edittext edittext nop = (edittext) findviewbyid(r.id.edittext); edittext cob = (edittext) findviewbyid(r.id.edittext2); //getting strings try{ x = double.valueof(nop.gettext().tostring()); } catch (numberformatexception) { x = 0; } try{ y = double.valueof(cob.gettext().tostring()); } catch (numberformatexception) { y = 0; } string textx = nop.gettext().tostring(); // if variable contains valid number: if (textx != null && textx.length() > 0) x = double.valueof(); // exception thrown method else x = 0; //textview final textview tv = (textview) findviewbyid(r.id.textview); btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { double z = x / y; tv.settext((int) z); } }); } }
here logcat:
08-19 07:30:28.452 16070-16070/com.elie.billsplitter e/androidruntime﹕ fatal exception: main process: com.elie.billsplitter, pid: 16070 java.lang.runtimeexception: unable start activity componentinfo{com.elie.billsplitter/com.elie.billsplitter.mainactivity}: java.lang.numberformatexception: invalid int: "" @ android.app.activitythread.performlaunchactivity(activitythread.java:2325) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2390) @ android.app.activitythread.access$800(activitythread.java:151) @ android.app.activitythread$h.handlemessage(activitythread.java:1303) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:135) @ android.app.activitythread.main(activitythread.java:5257) @ java.lang.reflect.method.invoke(native method) @ java.lang.reflect.method.invoke(method.java:372) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:698) caused by: java.lang.numberformatexception: invalid int: "" @ java.lang.integer.invalidint(integer.java:138) @ java.lang.integer.parseint(integer.java:358) @ java.lang.integer.parseint(integer.java:334) @ com.elie.billsplitter.mainactivity.oncreate(mainactivity.java:25) @ android.app.activity.performcreate(activity.java:5990) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1106) @ android.app.activitythread.performlaunchactivity(activitythread.java:2278) at android.app.activitythread.handlelaunchactivity(activitythread.java:2390) at android.app.activitythread.access$800(activitythread.java:151) at android.app.activitythread$h.handlemessage(activitythread.java:1303) at android.os.handler.dispatchmessage(handler.java:102) at android.os.looper.loop(looper.java:135) at android.app.activitythread.main(activitythread.java:5257) at java.lang.reflect.method.invoke(native method) at java.lang.reflect.method.invoke(method.java:372) at com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:903) at com.android.internal.os.zygoteinit.main(zygoteinit.java:698)
looking exception:
java.lang.runtimeexception: unable start activity componentinfo{com.elie.billsplitter/com.elie.billsplitter.mainactivity}: java.lang.numberformatexception: invalid int: ""
the error in piece of code
//getting strings x = integer.parseint(nop.gettext().tostring()); y = integer.parseint(cob.gettext().tostring());
why? nop.gettext().tostring().equals("")
or cob.gettext().tostring().equals("")
.
how avoid?
you must sure valid string
edittext
because if them empty or not contain equivaloent double
string
have numberformatexception
.
solution?
best ways be:
- adding validation
edittext
validstring
s. - convert value if
numberformatexception
thrown.
try{ x = double.valueof(nop.gettext().tostring()); } catch (numberformatexception e) { x = 0; } try{ y = double.valueof(cop.gettext().tostring()); } catch (numberformatexception e) { y = 1; // division / 0 throw nan }
add-on: why dont use integer.parseint()
??
if create double
s cannot parse int
s because lose accuracy, and, example in java (int) 1 / (int) 3 = 0
or (int) 10 / (int) 3 = 3
use double::valueof()
x = double.valueof(nop.gettext().tostring()); y = double.valueof(cob.gettext().tostring());
Comments
Post a Comment