Steven Sugiarto Wijaya Steven Sugiarto Wijaya - 3 months ago 9
Android Question

Application Stop when input editText more than 10 digit

i am trying to show calculation textView(txtHasil) it is running but when input more than 10 application suddenly force close. this is my code:

btnHitung.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

//String plafond = NumberTextWatcherForThousand.trimCommaOfString(edtPlafond.getText().toString());
String plafond = edtPlafond.getText().toString().trim();
String jasa = edtJasa.getText().toString().trim();

int edtPlafond = Integer.parseInt(plafond);
float edtJasa = Float.parseFloat(jasa);

double hasil = (edtPlafond * edtJasa )/100;


txtHasil.setText(""+hasil+"\nplafond: "+edtPlafond+"\nJasa: "+edtJasa);
//txtHasil.addTextChangedListener(new NumberTextWatcherForThousand((EditText) txtHasil));
}
}


i have been try change int,float, and double. i have been read this link: This program doesn't work properly for decimals more than 10 digits? but didn't help. any suggest will be help. thanks

Answer
Integer.parseInt(plafond);

This is the problem.It can not parse anythong larger than Integer.MAX_VALUE

int edtPlafond;
try {

    edtPlafond = Integer.parseInt(plafond);

} catch (NumberFormatException e ) {
   e.printStackTrace(); 
   // add proper error handling
}

The best would be to have a longer value - long...

long edtPlafond;
try {

    edtPlafond = Long.parseLong(plafond);

} catch (NumberFormatException e ) {
   e.printStackTrace();
   // add proper error handling
}

And an example of handling the error in a better way, by displaying the error in a dialog:

} catch (NumberFormatException e ) {
        new AlertDialog.Builder(getActivity())
          .setTitle("Error: incorrect number entered!")
          .setMessage("The exact error is: " + e.getMessage())
          .setPositiveButton("Ok",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int b) {
                        dialog.cancel();
                    }
                });
          .create()
          .show();
}

Note: all conversions need such a treatment...

Comments