Steven Sugiarto Wijaya Steven Sugiarto Wijaya - 1 year ago 61
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() {
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 Source

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

int edtPlafond;
try {

    edtPlafond = Integer.parseInt(plafond);

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

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

long edtPlafond;
try {

    edtPlafond = Long.parseLong(plafond);

} catch (NumberFormatException e ) {
   // 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())
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int b) {

Note: all conversions need such a treatment...