Title: Error when EditText is blank (unable to parse ' ' as integer)
Tags: java;android;numberformatexception
Question: I have a button and an edittext. The user enters a number and then clicks the button. Something then happens with that number.

However i get an error when the edittext is blank. How do i fix this? My code below is my attempt..but it doesn't work as i get the following error and my app closes:

E/AndroidRuntime(324): java.lang.NumberFormatException: unable to parse '' as integer

```public void onClick(View arg0) {
    // TODO Auto-generated method stub
    switch (arg0.getId()) {
    case R.id.bBankDeposit:
        deposit();
        break;
    case R.id.bBankWithdraw:
        withdraw();
        break;
    }
}

public void deposit() {
    String d = etDepositAmount.getText().toString();
    Integer depositAmount = Integer.valueOf(d);

    if (depositAmount &gt; playerCash | depositAmount <= 0
            | etDepositAmount.getText().toString() == "") {
        new AlertDialog.Builder(this).setTitle("Wait")
                .setMessage("Please enter a valid deposit value.")
                .setPositiveButton("OK", null).show();
        etDepositAmount.setText("");
    } else {
        int newBankBalance = playerBalance + depositAmount;
        playerBalance = newBankBalance;
        playerCash = (playerCash - depositAmount);

        bankBalance.setText("Bank Balance: $" + playerBalance);
        cash.setText("Cash in hand: $" + playerCash);
        etDepositAmount.setText("");

        Player stats = new Player(this);
        stats.open();
        stats.bankDeposit(playerId, playerCash, playerBalance);
        stats.close();
    }
}
```

}


Here is the accepted answer: The idea behind your solution is correct, you just have some minor syntax errors.

When you do ```Integer.valueOf(string)``` you need to handle a number format exception. It's just Java's way of letting you know it can reliably return an Integer for the given string.

```|``` is a bitwise OR operation. Presumably you want ```||```, which compares using boolean logic. Secondly, when comparing strings you want to compare by value, not by reference. ```==``` compares by reference, while ```someString.equals(someOtherString)``` will actually check the contents of the string. You really don't need to do that though because you've already parsed it into an Integer.

With those fixes you'll figure out if the input is incorrect and display an alert if necessary.

So - the full solution is ....

```public void deposit() {
    String d = etDepositAmount.getText().toString();
    Integer depositAmount = 0;
    try
    {
       depositAmount = Integer.valueOf(d);
    }
    catch(NumberFormatException ex)
    {
       // Uh oh! Bad input!
    }

    if (depositAmount &gt; playerCash || depositAmount <= 0) 
    {
        new AlertDialog.Builder(this).setTitle("Wait")
                .setMessage("Please enter a valid deposit value.")
                .setPositiveButton("OK", null).show();
        etDepositAmount.setText("");
    } else {
        int newBankBalance = playerBalance + depositAmount;
        playerBalance = newBankBalance;
        playerCash = (playerCash - depositAmount);

        bankBalance.setText("Bank Balance: $" + playerBalance);
        cash.setText("Cash in hand: $" + playerCash);
        etDepositAmount.setText("");

        Player stats = new Player(this);
        stats.open();
        stats.bankDeposit(playerId, playerCash, playerBalance);
        stats.close();
    }
}
```


Here is another answer: You have to check for the empty values of the edit-text to solve the ```NumberFormatException```

See this code for check the values for ```null``` or ```blank```

```if(!TextUtils.isEmpty(text)) {
    //Use it
}else {
    //display validation message
}
```


Here is another answer: ```if (!d.equals("")&amp;&amp;d!=null){
    Integer depositAmount = Integer.valueOf(d);
}
```

Comment for this answer: Or, as of API level 9, you can use [isEmpty()](http://developer.android.com/reference/java/lang/String.html#isEmpty()). And you may want to include an `else` statement in case `d` is empty.

Here is another answer: You have to catch ```NumberFormatException```

```String s ="";
int i=0;
try{
i=Integer.parseInt(s);

}catch(NumberFormatException ex){
//Some toast or alert
}
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 4, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2012:00:03/29/12', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<java><android><numberformatexception>', 'Title': "Error when EditText is blank (unable to parse ' ' as integer)", 'ViewCount': 1437, 'id': 9917486, 'user_AboutMe': '', 'user_AccountId': 1264820.0, 'user_DisplayName': 'gurpsone', 'user_Reputation': 391.0, 'user_Views': 32.0, 'user_id': 1222344.0}}