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;
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:
NumberFormatExtension
exception