Skip to content

Commit

Permalink
add utility methods to convert a string to Integer/Long without havin…
Browse files Browse the repository at this point in the history
…g to worry NumberFormatException
  • Loading branch information
darkv authored and Pascal Robert committed Apr 30, 2012
1 parent bdc1e92 commit 96300ad
Showing 1 changed file with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,41 @@ public static Integer integerWithString(String s) {
// ignore
}
return null;
}
}

/**
* Wrapper for {@link Integer#valueOf(String)} that catches
* the NumberFormatException.
*
* @param s string to convert to an Integer
* @return Integer or <code>null</code> if the string could
* not be parsed
*/
public static Integer safeInteger(String s) {
try {
return Integer.valueOf(s);
} catch (NumberFormatException e) {
// ignore
}
return null;
}

/**
* Wrapper for {@link Long#valueOf(String)} that catches
* the NumberFormatException.
*
* @param s string to convert to a Long
* @return Long or <code>null</code> if the string could
* not be parsed
*/
public static Long safeLong(String s) {
try {
return Long.valueOf(s);
} catch (NumberFormatException e) {
// ignore
}
return null;
}

/**
* Retrives a given string for a given name, extension
Expand Down

0 comments on commit 96300ad

Please sign in to comment.