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

Time:07-22

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. Can anyone help me solve this problem? Below is my code in java:

@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:

you should delele redundance word(else)

CodePudding user response:

Try this code

if (!matong_a1.getText().toString().isEmpty() && !spirulina_a2.getText().toString().isEmpty() && !ucga.getText().toString().isEmpty() && !thittho.getText().toString().isEmpty())
{
    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());
}
else
{
    if (matong_a1.getText().toString().isEmpty())
        matong_a1.setText("0");
    if (spirulina_a2.getText().toString().isEmpty())
        spirulina_a2.setText("0");
    if (ucga.getText().toString().isEmpty())
        ucga.setText("0");
    if (thittho.getText().toString().isEmpty())
        thittho.setText("0");
}

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());
}
  • Related