java - Creating dot-function for android calculator -
im doing small project, gathering random calculator codes net , improving them learn java , android development...
so, im trying implement dot-function calculator code, doesn't work it's supposed to. instead of adding dot, adds value 46 (why 46??). me create functional dot-function?
java code:
package com.example.elof_calculator; import android.os.bundle; import android.app.activity; import android.view.menu; import android.view.view; import android.widget.button; import android.widget.edittext; public class mainactivity extends activity { public string str =""; character op = 'q'; double i,num,numtemp; edittext showresult; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); showresult = (edittext)findviewbyid(r.id.result_id); } public void btn1clicked(view v){ insert(1); } public void btn2clicked(view v){ insert(2); } public void btn3clicked(view v){ insert(3); } public void btn4clicked(view v){ insert(4); } public void btn5clicked(view v){ insert(5); } public void btn6clicked(view v){ insert(6); } public void btn7clicked(view v){ insert(7); } public void btn8clicked(view v){ insert(8); } public void btn9clicked(view v){ insert(9); } public void btn0clicked(view v){ insert(0); } public void btndotclicked(view v){ insert('.'); //error } public void btnplusclicked(view v){ perform(); op = '+'; } public void btnminusclicked(view v){ perform(); op = '-'; } public void btndivideclicked(view v){ perform(); op = '/'; } public void btnmulticlicked(view v){ perform(); op = '*'; } public void btnequalclicked(view v){ calculate(); } public void btnclearclicked(view v){ reset(); } private void reset() { // todo auto-generated method stub str =""; op ='q'; num = 0; numtemp = 0; showresult.settext(""); } private void insert(int j) { // todo auto-generated method stub str = str+integer.tostring(j); num = integer.valueof(str).intvalue(); showresult.settext(str); } private void perform() { // todo auto-generated method stub str = ""; numtemp = num; } private void calculate() { // todo auto-generated method stub if(op == '+') num = numtemp+num; else if(op == '-') num = numtemp-num; else if(op == '/') num = numtemp/num; else if(op == '*') num = numtemp*num; showresult.settext(""+num); } }
xml code dot-button:
<button android:id="@+id/btndot_id" style="?android:attr/buttonstylesmall" android:layout_width="70dp" android:layout_height="64dp" android:background="@drawable/buttonstyle1" android:gravity="center_vertical|center_horizontal" android:text="." android:textcolor="@android:color/white" android:textsize="22sp" android:onclick="btndotclicked" />
here java function , code dot-function specifically, main error:
public void btndotclicked(view v){ insert('.'); }
so, how should code dot-function be? appreciated :))
the problem insert function has signature void insert(int j)
in java implicit widening conversion take place, if use char
primitive type when int
type expected. example valid java:
int dot = '.';
however, value of int
variable dot
46 happens integer corresponds bit pattern used represent '.'
.
what can do, change insert
function accept string
parameters , add overload int
parameters. need change logic around num
since can have floating point numbers.
private void insert(int value) { insert(integer.tostring(value)); ... num = double.parsedouble(str); } private void insert(string value) { ... } ... public void btndotclicked(view v){ insert("."); // inserting string instead of char }
Comments
Post a Comment