Home > Software design >  java.lang.NumberFormatException: For input string in Java Android Converting String to Integer
java.lang.NumberFormatException: For input string in Java Android Converting String to Integer

Time:10-03

I know this is common to ask but I don't know why my app crashes, I just want to convert string to integer but it seems it needs to be done in NumberFormatException. The error begins with this line

error line

int number = Integer.parseInt(amt);

enter image description here

My java code

 SharedPreferences sh = getSharedPreferences("MySharedPref", MODE_APPEND);
 String nma_amount = sh.getString("nma_amount", "");
 edt_nma_amount.setText(nma_amount);
            
 String amt = edt_nma_amount.getText().toString();
 if (!TextUtils.isEmpty(amt)) {
     int number = Integer.parseInt(amt);
     if (number >= 100) {
         tilNmaReason.setVisibility(View.VISIBLE);
     } else {
          tilNmaReason.setVisibility(View.GONE);
     }
 }
 else{
      Log.v(ContentValues.TAG,"empty");
 }

CodePudding user response:

Because the number in question is bigger than the limit of Integer Change Integer to Long or Double and try again

 SharedPreferences sh = getSharedPreferences("MySharedPref", MODE_APPEND);
 String nma_amount = sh.getString("nma_amount", "");
 edt_nma_amount.setText(nma_amount);
            
 String amt = edt_nma_amount.getText().toString();
 if (!TextUtils.isEmpty(amt)) {
     long number = Long.parseLong(amt);
     if (number >= 100) {
         tilNmaReason.setVisibility(View.VISIBLE);
     } else {
          tilNmaReason.setVisibility(View.GONE);
     }
 }
 else{
      Log.v(ContentValues.TAG,"empty");
 }
  • Related