Why is this code throwing NumberFormatException In

2019-08-10 08:28发布

问题:

I'm trying to get it to if a user doesn't enter a value in the EditText boxes, the initial value is set to 0 (to prevent the crash error NumberFormatException Invalid int: "") which is thrown assuming because there is no integer value to read, since the user didn't input one in this case.

I've tried a number of things most recently this:

String boozeAmount = boozeConsumed.getText().toString();

        if (boozeAmount == "" || boozeAmount == null){
            boozeConsumed.setText("0");
            boozeAmount = boozeConsumed.getText().toString();
        }
        int boozeOz = Integer.parseInt(boozeAmount) * 12;
        double beerCalc = boozeOz * 4 * 0.075;

But it seems to still throw the same error, not sure why the int values aren't being set to 0?

Throwing error on

int boozeOz = Integer.parseInt(boozeAmount) * 12;

回答1:

if (boozeAmount == "" || boozeAmount == null){

It can be easily deduced from your explanation that this particular if statement is returning true, that's why your string value is not being set to "0".

Strings in Java are not primitive, i.e they cannot be compared with the comparator ==. You need to use the method equals to compare strings, as in boozeAmount.equals("").

Apache Commons has a StringUtils utility that can check if strings are null or empty. Check out isEmpty and isBlank.



回答2:

you dont compare strings with == you compare it with .equals()



回答3:

Change it to:

if (boozeAmount.equals("") || boozeAmount == null)

Although it's probably safest to also do:

 if (...)
 {
   // just set it to zero and skip even trying to parse
 }
 else
 {
   // do the actual parsing
 }


回答4:

Whenever you are fetching a string value from the EditText always trim that variable to avoid any white spaces from EditText.

String boozeAmount = boozeConsumed.getText().toString().trim();                  // apply Trim

// Always compare strings with `equals()` method in Java & Android
if ( boozeAmount.equals( "" ) || boozeAmount == null )
{
    boozeConsumed.setText("0");
    boozeAmount = boozeConsumed.getText().toString();
}
    int boozeOz = Integer.parseInt(boozeAmount) * 12;
    double beerCalc = boozeOz * 4 * 0.075;


回答5:

You could check if it's blank and then fill it with 0 and then get the input.

Also, always compare string values with .equals and use .trim() to get rid of whitespace so that it's recognized as invalid input as well.

if (boozeConsumed.getText().toString().trim().equals("")) {
// if (boozeConsumed.length() == 0) { // doesn't consider spaces though
    boozeConsumed.setText("0");
}

String boozeAmount = boozeConsumed.getText().toString();
int boozeOz = Integer.parseInt(boozeAmount) * 12;
double beerCalc = boozeOz * 4 * 0.075;

Or just do this, because you don't need to parse it to an integer if you know there's nothing there:

String boozeAmount = boozeConsumed.getText().toString();

int boozeOz = 0;
double beerCalc = 0; 

if (boozeAmount.trim().equals("") || boozeAmount == null){
    boozeConsumed.setText("0");
    boozeAmount = boozeConsumed.getText().toString();
} else {
    // only parse it if there's something there
    boozeOz = Integer.parseInt(boozeAmount) * 12;
    beerCalc = boozeOz * 4 * 0.075;
}