Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

java.lang.NumberFormatException: For input string in Java Android Converting String to Integer

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);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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");
 }

>Solution :

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");
 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading