I am trying to write a very simple Android app.
It takes in 2 floating point numbers, does a calculation,
When I run my app, it crashes.
However, if I enter integers instead of floating point numbers, my
program doesn’t crash and does the correct calculation.
How to solve this problem? thx a lot
private Button.OnClickListener btnTranListener =new Button.OnClickListener(){
public void onClick(View v){
int amount=Integer.parseInt(editfc.getText().toString());
double $=1.24*(double)amount;
$ = (double) (((int)($ * 1000)) / 1000.0);
outelc124.setText("ELC(1.24)= " + $ + " /pc");
Replace
Integer.parseInt()withDouble.parseDouble()and you will be able to handle numbers with decimals or integers.You can also catch
NumberformatExceptionto keep it from crashing if they enter other bogus input.