0

So part of my application are various calculators which I coded successfully, but if user does not input any numbers in EditText fields, application force closes.

The second EditText field must not have value greater than 12, and Toast works there, but what should I write so the Toast is printed if user does not input values in first EditText or second, or both?

Here's my code so you can see what I've tried:

public class Kalkulatori1RM extends AppCompatActivity {
EditText polje1, polje2;
TextView rezultat1RM;
Button btnIzracunaj1RM;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_kalkulatori1_rm);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    Intent intent = getIntent();}

    public boolean isEmpty(EditText etText) {





    polje1 = (EditText) findViewById(R.id.edit1rmTezina);
    polje2 = (EditText) findViewById(R.id.edit1rmPonavljanja);

    rezultat1RM = (TextView) findViewById(R.id.text1RMJe);

    btnIzracunaj1RM = (Button) findViewById(R.id.btnIzracunaj1RM);



    btnIzracunaj1RM.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(isEmpty(polje1)||isEmpty(polje2)){
                Toast.makeText(Kalkulatori1RM.this, "Both inputs must be provided prior to calculating",Toast.LENGTH_LONG).show();
            } else {

            double prvoPolje = Double.parseDouble(polje1.getText().toString());
            double drugoPolje = Double.parseDouble(polje2.getText().toString());

            double rezultat = 0;


            if (drugoPolje == 1) {
                rezultat = prvoPolje * 1;

            } else if (drugoPolje == 2) {
                rezultat = prvoPolje * 1.05;

            } else if (drugoPolje == 3) {
                rezultat = prvoPolje * 1.08;

            } else if (drugoPolje == 4) {
                rezultat = prvoPolje * 1.11;

            } else if (drugoPolje == 5) {
                rezultat = prvoPolje * 1.15;

            } else if (drugoPolje == 6) {
                rezultat = prvoPolje * 1.18;

            } else if (drugoPolje == 7) {
                rezultat = prvoPolje * 1.20;

            } else if (drugoPolje == 8) {
                rezultat = prvoPolje * 1.25;

            } else if (drugoPolje == 9) {
                rezultat = prvoPolje * 1.30;

            } else if (drugoPolje == 10) {
                rezultat = prvoPolje * 1.33;

            } else if (drugoPolje == 11) {
                rezultat = prvoPolje * 1.37;

            } else if (drugoPolje == 12) {
                rezultat = prvoPolje * 1.43;

            } else if (drugoPolje > 12) {
                Toast.makeText(getApplicationContext(), "Broj ponavljanja ne smije biti veći od 12", Toast.LENGTH_LONG).show();

            } else {
                Toast.makeText(Kalkulatori1RM.this, "Unesite odgovarajuće podatke prije nego što pritisnete na 'Izračunaj'", Toast.LENGTH_LONG).show();
            }

            rezultat = Math.round(rezultat * 100.0) / 100.0;
            rezultat1RM.setText(Double.toString(rezultat));


        }
    }

    });

    return etText.getText().toString().trim().length() == 0;

}

Any ideas?

This is the error:

02-17 20:35:08.454 1609-1609/hr.app.liftme.liftmehr E/AndroidRuntime: FATAL EXCEPTION: main
                                                                      Process: hr.app.liftme.liftmehr, PID: 1609
                                                                      java.lang.NumberFormatException: Invalid double: ""
                                                                          at java.lang.StringToReal.invalidReal(StringToReal.java:63)
                                                                          at java.lang.StringToReal.parseDouble(StringToReal.java:267)
                                                                          at java.lang.Double.parseDouble(Double.java:301)
                                                                          at hr.app.liftme.liftmehr.Kalkulatori1RM$1.onClick(Kalkulatori1RM.java:51)
                                                                          at android.view.View.performClick(View.java:4856)
                                                                          at android.view.View$PerformClick.run(View.java:19956)
                                                                          at android.os.Handler.handleCallback(Handler.java:739)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                          at android.os.Looper.loop(Looper.java:211)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5389)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:372)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
DaxHR
  • 683
  • 1
  • 11
  • 30

3 Answers3

2

The application force closes because of the Double.parseDouble that throws NumberFormatException if the string does not contain a parsable double. You have to catch the exception yourself, like shown here.

Community
  • 1
  • 1
TDG
  • 5,909
  • 3
  • 30
  • 51
2

If you checked Logcat you would likely see that you're attempting to parse null or blank string to a double via Double.parseDouble(...).

It's best to check your input prior to performing your calculations, as such....

//Place in a global reference (static Utils class or something)
public boolean isEmpty(EditText etText) {
  return etText.getText().toString().trim().length() == 0;
}


btnIzracunaj1RM.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
     if(isEmpty(polje1)||isEmpty(polje2)){
        Toast.makeText(this,"Both inputs must be provided prior to calculating",Toast.LENGTH_LONG).show();
     } else {
       double prvoPolje = Double.parseDouble(polje1.getText().toString());
        double drugoPolje = Double.parseDouble(polje2.getText().toString());

        double rezultat = 0;


        if (drugoPolje == 1) {
            rezultat = prvoPolje * 1;

        } else if (drugoPolje == 2) {
            rezultat = prvoPolje * 1.05;

        } else if (drugoPolje == 3) {
            rezultat = prvoPolje * 1.08;

        } else if (drugoPolje == 4) {
            rezultat = prvoPolje * 1.11;

        } else if (drugoPolje == 5) {
            rezultat = prvoPolje * 1.15;

        } else if (drugoPolje == 6) {
            rezultat = prvoPolje * 1.18;

        } else if (drugoPolje == 7) {
            rezultat = prvoPolje * 1.20;

        } else if (drugoPolje == 8) {
            rezultat = prvoPolje * 1.25;

        } else if (drugoPolje == 9) {
            rezultat = prvoPolje * 1.30;

        } else if (drugoPolje == 10) {
            rezultat = prvoPolje * 1.33;

        } else if (drugoPolje == 11) {
            rezultat = prvoPolje * 1.37;

        } else if (drugoPolje == 12) {
            rezultat = prvoPolje * 1.43;

        } else if (drugoPolje > 12) {
            Toast.makeText(getApplicationContext(), "Broj ponavljanja ne smije biti veći od 12", Toast.LENGTH_LONG).show();

        } else {
            Toast.makeText(Kalkulatori1RM.this, "Unesite odgovarajuće podatke prije nego što pritisnete na 'Izračunaj'", Toast.LENGTH_LONG).show();
        }

        rezultat = Math.round(rezultat * 100.0) / 100.0;
        rezultat1RM.setText(Double.toString(rezultat));

     }
   }
})

Another way would be to replace isEmpty(...) calls with something like tryParseDouble...

public boolean tryParseDouble(String str){
    try{

      double parsed = Double.parseDouble(str);
      return true;
    } catch (Exception e){
      return false;
    }
}

Which might be a better method.

em_
  • 2,134
  • 2
  • 24
  • 39
  • @DaxHR yes it says ` java.lang.NumberFormatException: Invalid double: ""` meaning you're attempting to parse a blank string. Following my implementation will solves your problem – em_ Feb 17 '16 at 19:43
  • Did that, get a ton of errors that it can't resolve isEmpty and other stuff – DaxHR Feb 17 '16 at 19:45
  • Did you include the `isEmpty` method I wrote? If you didn't, it won't compile... It's important to seek out reasons why you come across errors instead of immediately looking for help. – em_ Feb 17 '16 at 20:11
  • I did. Now the app launches but the button does not do anything. See my OP, the code is updated. Had to put that return value to the end because it was giving me error – DaxHR Feb 17 '16 at 20:20
  • Why did you wrap all your logic within `isEmpty`? That's why you had to place that return value there, because it wants a `boolean` value returned... Make sure you squiggly brackets `{}` are wrapped around what they're suppose to. I'll update my code to show you exactly what you should have. – em_ Feb 17 '16 at 20:26
  • Now I get error cannot resolve simbol setOnClickListener – DaxHR Feb 17 '16 at 20:30
1

Since you have a finite number of specific choices for one of the inputs, I suggest using a Spinner instead of a TextView. This way you can display the exact values used in the calculation and avoid confusion by your users. You also no longer need to worry about erroneous input.

For any EditText which allows any number to be entered, you need to ensure that the input is valid. You can do this in one of two ways:

  1. Catch the NumberFormatException and display an error message.

  2. Set the inputType for the EditText to number.

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268