How to check if a String is numeric in Java

To check if a String is numeric in Java, you can use the isNumeric method of the StringUtils class from the org.apache.commons.lang3 library.

Here's an example:

String str = "12345";
boolean isNumeric = StringUtils.isNumeric(str);

If the str variable contains a numeric value, the isNumeric variable will be true. Otherwise, it will be false.

You can also use the isCreatable method of the NumberUtils class from the same library to check if a String can be converted to a number:

String str = "12345";
boolean isNumeric = NumberUtils.isCreatable(str);

If the str variable contains a numeric value, the isNumeric variable will be true. Otherwise, it will be false.

Alternatively, you can use the Double.parseDouble method to check if a String can be parsed to a double value:

String str = "12345";
boolean isNumeric = true;
try {
    Double.parseDouble(str);
} catch (NumberFormatException e) {
    isNumeric = false;
}

If the str variable contains a numeric value, the isNumeric variable will be true. Otherwise, it will be false.