I am new to android development and I am trying to develop a calculator with single edit text field. It's not showing any compiler error but it crashes as soon as I press any button. I'm first taking the input for num1 and clearing the edit text field followed by a switch case to detect the operation and collecting the data of the second number.
public class MainActivity extends Activity implements OnClickListener{
EditText ino;
Button btnadd;
Button btnsub;
Button btnmult;
Button btndiv;
Button btneq;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ino=(EditText) findViewById(R.id.ino);
btnadd=(Button) findViewById(R.id.add);
btndiv=(Button) findViewById(R.id.div);
btnsub=(Button) findViewById(R.id.sub);
btnmult=(Button) findViewById(R.id.mult);
btneq=(Button) findViewById(R.id.eq);
btnadd.setOnClickListener((OnClickListener) this);
btnsub.setOnClickListener(this);
btnmult.setOnClickListener(this);
btndiv.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
float num1=0,num2=0,res=0;
String xyz="",ans;
num1=Float.parseFloat(ino.getText().toString());
ino.setText(xyz);
switch(v.getId())
{
case R.id.add:
num2=Float.parseFloat(ino.getText().toString());
res=num2+num1;
ans=Float.toString(res);
break;
case R.id.div:
num2=Float.parseFloat(ino.getText().toString());
ino.setText(xyz);
res=num1/num2;
ans=Float.toString(res);
ino.setText(ans);
break;
case R.id.mult:
num2=Float.parseFloat(ino.getText().toString());
ino.setText(xyz);
res=num2*num1;
ans=Float.toString(res);
ino.setText(ans);
break;
case R.id.sub:
num2=Float.parseFloat(ino.getText().toString());
ino.setText(xyz);
res=num1-num2;
ans=Float.toString(res);
ino.setText(ans);
break;
case R.id.eq:
ans=Float.toString(res);
ino.setText(ans);
break;
default: break;
}
}
}
Here is the copy of stack trace.
12-28 00:49:52.646 2125-2125/com.example.rishabh.calculator2
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.rishabh.calculator2, PID: 2125
java.lang.NumberFormatException: Invalid float: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseFloat(StringToReal.java:308)
at java.lang.Float.parseFloat(Float.java:306)
at com.example.rishabh.calculator2.MainActivity.onClick(MainActivity.java:56)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)