You can use the NumberUtils from Apache Commons. It's null-safe and you can optionally specify a default value.
Example:
NumberUtils.toLong(null) = 0L
NumberUtils.toLong("") = 0L
NumberUtils.toLong("1") = 1L
NumberUtils.toLong(null, 1L) = 1L
NumberUtils.toLong("", 1L) = 1L
NumberUtils.toLong("1", 0L) = 1L
For more info, check the API.
Answer from Ehler on Stack OverflowYou can use the NumberUtils from Apache Commons. It's null-safe and you can optionally specify a default value.
Example:
NumberUtils.toLong(null) = 0L
NumberUtils.toLong("") = 0L
NumberUtils.toLong("1") = 1L
NumberUtils.toLong(null, 1L) = 1L
NumberUtils.toLong("", 1L) = 1L
NumberUtils.toLong("1", 0L) = 1L
For more info, check the API.
When we say nullSafe we usually expect to receive a null from the API when passing null to it, not a 0 or -1 or any other value.
The intention is that the API does not break in case of receiving null, but simply returning null.
For this, use createLong static method of the org.apache.commons.lang3.math.NumberUtils.
CAUSION*:
This throws NumberFormatException if the input String is NOT null and is NOT numeric.
CAUSION**: since 3.1 it handles hex (0Xhhhh) and octal (0ddd) notations. N.B. a leading zero means octal; spaces are not trimmed.
Sample:
NumberUtils.createLong(null) --> null
NumberUtils.createLong("1") --> 1
NumberUtils.createLong("hi") --> NumberFormatException
NumberUtils.createLong(" 11") --> NumberFormatException (does not trim)
NumberUtils.createLong("023") --> 19 (leading 0 means octal)
NumberUtils.createLong("0x23") --> 35 (leading 0x means hex)
I'm afraid the value is not 1454. The value is null, as evidenced by this line:
java.lang.NumberFormatException: null
You need to check your parameter name, and make sure you've got it right, including case and invisible characters.
java.lang.NumberFormatException: null
If String dxorderId is null then it will throw NumberFormatException
Solution
Check dxorderId is not null and not empty string
if(dxorderId != null && !dxorderId.equals(""))
{
long orderid = Long.parseLong((dxorderId));
}
From oracle docs NumberFormatException
public class NumberFormatException
extends IllegalArgumentException
Thrown to indicate that the application has attempted to convert a string to one of the numeric types, but that the string does not have the appropriate format.
Apache Commons Lang has the class org.apache.commons.lang3.math.NumberUtils with convenient methods for convert. In another way, you can specify a default value if there is a error. e.g.
NumberUtils.toLong("") => 0L
NumberUtils.toLong(null, 1L) => 1L
NumberUtils.toByte(null) => 0
NumberUtils.toByte("1", 0) => 1
Guava has several tryParse methods that return null on a failed parse, e.g. Ints.tryParse, Floats.tryParse, etc
You can create rather complex regular expression but it isn't worth that. Using exceptions here is absolutely normal.
It's natural exceptional situation: you assume that there is an integer in the string but indeed there is something else. Exception should be thrown and handled properly.
If you look inside parseLong code, you'll see that there are many different verifications and operations. If you want to do all that stuff before parsing it'll decrease the performance (if we are talking about parsing millions of numbers because otherwise it doesn't matter). So, the only thing you can do if you really need to improve performance by avoiding exceptions is: copy parseLong implementation to your own function and return NaN instead of throwing exceptions in all correspondent cases.
From commons-lang StringUtils:
public static boolean isNumeric(String str) {
if (str == null) {
return false;
}
int sz = str.length();
for (int i = 0; i < sz; i++) {
if (Character.isDigit(str.charAt(i)) == false) {
return false;
}
}
return true;
}
here is a solution :
int tryParseInt(String value) {
try {
return Integer.parseInt(value);
} catch(NumberFormatException nfe) {
// Log exception.
return 0;
}
}
you should catch NumberFormatException instead of exception.
I'd consider using NumberUtils.toInt from Apache Commons Lang which does exactly this:
public static int NumberUtils.toInt(java.lang.String str, int defaultValue)
The implementation uses the already mentioned Integer.parseInt with an additional null check.
See also: Effective Java, 2nd edition, Item 47: Know and use the libraries (The author mentions only the JDK's built-in libraries but I think the reasoning could be true for other libraries too.)