I like the suggestion in String to Int in java - Likely bad data, need to avoid exceptions to implement a utility method that parses an int, but returns a default value if the string cannot be parsed.
public static int parseInt(String s, int defaultValue) {
if (s == null) return defaultValue;
try {
return Integer.parseInt(s);
} catch (NumberFormatException x) {
return defaultValue;
}
}
Is there an existing open source library (from apache commons, or google, for example) that implements this as well as for other data types such as boolean, float, double, long, etc?