I’m new to Java. I created this code in order to check input field for string or number.
try {
int x = Integer.parseInt(value.toString());
} catch (NumberFormatException nFE) {
// If this is a string send error message
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR,
" " + findValue + " must be number!", null));
}
How I can create the same check for number but using just if(){} without try-catch?
You can use a
patternwithString#matchesmethod: –"[-]?\\d+"pattern will match any sequence ofdigits, preceded by an optional-sign."\\d+"means match one or more digits.