Proper way to avoid parseInt throwing a NumberFormatException for input string: ""
Asked Answered
G

7

20

When I run parseInt:

Integer.parseInt(myString);

it throws:

NumberFormatException: For input string: ""

Does this mean I have do something like this?

if(StringUtils.isNotBlank(myString))
  return Integer.parseInt(myString);
else
 return 0;
Galba answered 8/1, 2012 at 20:24 Comment(0)
C
23

Well, you could use the conditional operator instead:

return StringUtils.isNotBlank(myString) ? Integer.parseInt(myString) : 0;

If you need to do this in multiple places, you'd probably want to put this into a separate method. Note that you should also consider situations where myString is null, or contains non-numeric text.

Conciliate answered 8/1, 2012 at 20:26 Comment(0)
D
32

Yes, but: Wrap it in a thin method (and eliminate the redundant else), or use an existing implementation, like Commons Lang's NumberUtils.toInt(str, defaultValue):

NumberUtils.toInt(myString, 0);

This method handles null values and conversion failures.

Writing the same thing on your own is straight-forward:

  • Check for null, and/or...
  • ...Wrap the NumberFormatExtension exception
Dirichlet answered 8/1, 2012 at 20:30 Comment(0)
C
23

Well, you could use the conditional operator instead:

return StringUtils.isNotBlank(myString) ? Integer.parseInt(myString) : 0;

If you need to do this in multiple places, you'd probably want to put this into a separate method. Note that you should also consider situations where myString is null, or contains non-numeric text.

Conciliate answered 8/1, 2012 at 20:26 Comment(0)
E
9

If the string can be empty I do it this way:

Integer.parseInt("0" + inputString)

When I'm not sure it contains only digits:

Integer.parseInt(0 + inputString.replaceAll("\\D+",""))
Excipient answered 19/2, 2016 at 14:38 Comment(4)
I might be a novice but why is this not upvoted at all, such a neat simple trick. Update, just realized why, it fails for negative values!Blackbeard
This tricky way only work for unsigned integer (positive number)Seigneury
Correct. For general case I would use some third-party *Utils library (see other answers)Excipient
Blindly replacing non-digits can lead to unexpected behavior in the eyes of the user.Dirichlet
H
4

What you have is fine, but as a coding style I prefer to make tests "positive" (isBlank), rather than "negative" (isNotBlank), ie

if (StringUtils.isBlank(myString)) {
    return 0;
}
return Integer.parseInt(myString); // Note: No need for else when the if returns

or, more succinctly:

return StringUtils.isBlank(myString) ? 0 : Integer.parseInt(myString);
Hoarse answered 8/1, 2012 at 20:31 Comment(0)
I
0

Yes. (Validate your inputs before making assumptions about what are in them. :-)

+1 for already finding Apache's common-lang w/ StringUtils.

Intercollegiate answered 8/1, 2012 at 20:26 Comment(0)
D
0

Integer.parseInt(String) doesn't accept non-numeric input, including nulls and empty strings.

Either guard against that like you suggested, or catch the NFE.

Donate answered 8/1, 2012 at 20:26 Comment(0)
I
0

I don't know why was I searching for this but here's the easy way:

int test=str.isEmpty()?0:Integer.parseInt(str);
Indene answered 8/11, 2013 at 12:37 Comment(1)
This will throw an NPE if str is null.Dirichlet

© 2022 - 2024 — McMap. All rights reserved.