Home > front end >  java.lang.NumberFormatException: empty String of editText
java.lang.NumberFormatException: empty String of editText

Time:07-24

I am making calculator app and receive error:

java.lang.NumberFormatException: empty String

I try some solution but they don't work for my app.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_nutrition);

    matong_a1 = findViewById(R.id.matong_a1);
    spirulina_a2 = findViewById(R.id.spirulina_a2);
    ucga = findViewById(R.id.ucga);
    btn = findViewById(R.id.btn);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            senddata();
        }
    });
}

private void senddata() {
    if (matong_a1.getText().toString().isEmpty()){
        matong_a1.setText("0");
    }
    else if (spirulina_a2.getText().toString().isEmpty()){
        spirulina_a2.setText("0");
    }
    else if (ucga.getText().toString().isEmpty()){
        ucga.setText("0");
    }
    else if ( thittho.getText().toString().isEmpty()){
        thittho.setText("0");
    }
    honey = (int) Double.parseDouble(matong_a1.getText().toString().trim());

    spirulina = (int) Double.parseDouble(spirulina_a2.getText().toString().trim());
    chicken = (int) Double.parseDouble(ucga.getText().toString().trim());
    rabbit = (int) Double.parseDouble(thittho.getText().toString().trim());

When user leave blank in edit text and press button (btn) to calculate result, I receive below error:

 java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1842)
    at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
    at java.lang.Double.parseDouble(Double.java:538)
    at com.tonyapp.healthy.Nutrition.senddata(Nutrition.java:434)
    at com.tonyapp.healthy.Nutrition.access$000(Nutrition.java:16)
    at com.tonyapp.healthy.Nutrition$1.onClick(Nutrition.java:151)
    at android.view.View.performClick(View.java:7448)
    at android.view.View.performClickInternal(View.java:7425)
    at android.view.View.access$3600(View.java:810)
    at android.view.View$PerformClick.run(View.java:28305)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

CodePudding user response:

use it like this

else if ( thittho.getText().toString().isEmpty()){

        thittho.setText("0");

    }else{


        honey = (int) Double.parseDouble(matong_a1.getText().toString().trim());


        spirulina = (int) Double.parseDouble(spirulina_a2.getText().toString().trim());
        chicken = (int) Double.parseDouble(ucga.getText().toString().trim());
        rabbit = (int) Double.parseDouble(thittho.getText().toString().trim());
}

CodePudding user response:

Use try-catch block like below

try {
    honey = (int)Double.parseDouble(matong_a1.getText().toString().trim());
} catch (NumberFormatException e) {
    matong_a1.setText("0");
}
try {
    spirulina = (int)Double.parseDouble(spirulina_a2.getText().toString().trim());
} catch (NumberFormatException e) {
    spirulina_a2.setText("0");
}
try {
    chicken = (int)Double.parseDouble(ucga.getText().toString().trim());
} catch (NumberFormatException e) {
    ucga.setText("0");
}
try {
    rabbit = (int)Double.parseDouble(thittho.getText().toString().trim());
} catch (NumberFormatException e) {
    thittho.setText("0");
}
  • Related