Using Apache Commons Library
Apache Commons library provides some methods to check whether a string is numeric or not.
NumberUtils.isCreatable() Method
The isCreatable() method is a simple and convenient method to check if a string is numeric. It also accepts numeric strings of hexadecimal numbers that start with 0x or oX, octal numbers that start with 0, scientific notations that use the letter e, and also accepts numbers marked with type qualifier.
import org.apache.commons.lang3.math.NumberUtils;
public static void main(String[] args) {
String num1 = "a10c";
String num2 = "-104";
String num3 = "100";
String num4 = "0xA10";
System.out.println("String " + num1 + " is numeric: " + NumberUtils.isCreatable(num1));
System.out.println("String " + num2 + " is numeric: " + NumberUtils.isCreatable(num2));
System.out.println("String " + num3 + " is numeric: " + NumberUtils.isCreatable(num3));
System.out.println("String " + num4 + " is numeric: " + NumberUtils.isCreatable(num4));
}
NumberUtils.isParsable() Method
The isParsable() method is used to check whether a string is parsable or not. It cannot work with hexadecimal numbers or scientific notations like isCreatable() method.
import org.apache.commons.lang3.math.NumberUtils;
public static void main(String[] args) {
String num1 = "a10c";
String num2 = "-104";
String num3 = "100";
String num4 = "0xA10";
System.out.println("String " + num1 + " is numeric: " + NumberUtils.isParsable(num1));
System.out.println("String " + num2 + " is numeric: " + NumberUtils.isParsable(num2));
System.out.println("String " + num3 + " is numeric: " + NumberUtils.isParsable(num3));
System.out.println("String " + num4 + " is numeric: " + NumberUtils.isParsable(num4));
}
StringUtils.isNumeric() Method
The isNumeric() method checks for Unicode digits and returns false if the numeric string denotes a negative number or contains a decimal point. This method should only be considered if we are just checking for positive integers.
import org.apache.commons.lang3.StringUtils;
public static void main(String[] args) {
String num1 = "a10c";
String num2 = "-104";
String num3 = "100";
String num4 = "0.11";
System.out.println("String " + num1 + " is numeric: " + StringUtils.isNumeric(num1));
System.out.println("String " + num2 + " is numeric: " + StringUtils.isNumeric(num2));
System.out.println("String " + num3 + " is numeric: " + StringUtils.isNumeric(num3));
System.out.println("String " + num4 + " is numeric: " + StringUtils.isNumeric(num4));
}
StringUtils.isNumericSpace() Method
The isNumericSpace() method also checks for space. If a string is of type "19 8" then this method will return true. It will also return true if the string is composed of just whitespace.
import org.apache.commons.lang3.StringUtils;
public static void main(String[] args) {
String num1 = "a10c";
String num2 = " ";
String num3 = "100";
String num4 = "0.11";
System.out.println("String " + num1 + " is numeric: " + StringUtils.isNumeric(num1));
System.out.println("String " + num2 + " is numeric: " + StringUtils.isNumeric(num2));
System.out.println("String " + num3 + " is numeric: " + StringUtils.isNumeric(num3));
System.out.println("String " + num4 + " is numeric: " + StringUtils.isNumeric(num4));
}