java - Calculator in Android Studio -
i have been trying build simple calculator in android studio.i beginner , have basic knowledge of java.i trying accept values in single text field instead of using 2 different text field.i have used on click listener on operator buttons , inside on equals button.but application closes click on operator buttons.how can use single text view 2 accept 2 numbers or more , apply mathematical operator on them?
edit:as have stated have little knowledge of java.i did google search before posting question.
add.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { n.settext(""); final float = float.parsefloat(n.gettext().tostring()); n.settext(""); equals.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { float b = float.parsefloat(n.gettext().tostring()); float c = + b; ans.settext("" + c); n.settext(""); } }); } });
on first line do:
n.settext("");
only after read text with
n.gettext().tostring()
which returns empty string passed parsefloat(). parsefloat() not expect empty string throws exception , application closes.
so, not settext("") before read value need.
more generally, consider saving current state each time there click. can, example, enable equal button after number typed, operator button clicked , number typed.
Comments
Post a Comment