Signup/Sign In

How to Check if a String is Numeric in Java?

A string is a very frequently used data type and can be used for various tasks. Whenever we take input from a text field or text area then that input will always be a string. Let's say we need to check whether the input from a text field is a valid number or not.

In this tutorial, we will learn how to check if a string is a valid number or not.

Check if a String is Numeric by Parsing

Parsing a string is probably the easiest and the best method to tell whether the string is numeric or not. Some common built-in parsing methods are shown below.

  • Integer.parseInt(String number)
  • Double.parseDouble(String number)
  • Float.parseFloat(String number)
  • Long.parseLong(String number)

These methods will parse the string and convert it to the corresponding number data type(like Double, or Integer).

If these methods do not throw a NumberFormatException then we can safely assume that the string is numeric.

Note that these methods will throw a NullPointerException if a null string is passed. The following code demonstrates this scenario.

public static void main(String[] args)
{
	String num = null;
	Double d = Double.parseDouble(num);
}


Exception in thread "main" java.lang.NullPointerException
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1838)
at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.base/java.lang.Double.parseDouble(Double.java:549)
at NumericString.main(NumericString.java:21)

Example: Using parseDouble() Method

Let's write our own method using the parseDouble() method. We will use try-catch blocks to detect and catch the NumberFormatException. If this exception is thrown then the method should return false.

public static boolean isStringNumeric(String number)
{
	boolean isNumeric;
	if(number == null)
		isNumeric = false;
	else
	{
		try
		{
			Double num = Double.parseDouble(number);
			isNumeric = true;
		}
		catch(NumberFormatException e)
		{
			isNumeric = false;
		}
	}
	return isNumeric;
}
public static void main(String[] args)
{
	String num1 = "1001";
	String num2 = "-101";
	String num3 = "1a10";
	String num4 = null;
		
	System.out.println("String " + num1 + " is numeric: " + isStringNumeric(num1));
	System.out.println("String " + num2 + " is numeric: " + isStringNumeric(num2));
	System.out.println("String " + num3 + " is numeric: " + isStringNumeric(num3));
	System.out.println("String " + num4 + " is numeric: " + isStringNumeric(num4));	
}


String 1001 is numeric: true
String -101 is numeric: true
String 1a10 is numeric: false
String null is numeric: false

Check if a String is Numeric by using Regular Expressions

We can write regular expressions to match our strings and tell whether they are numeric or not. We will use the matches() method of strings to compare the string number with the regular expression. We will use the regex "-?\\d+(\\.\\d+)?" to match strings. Let's try to understand this regex.

  • The -? part of the regex checks whether is the number is negative or not by looking for the hyphen or minus sign(-). The question mark is used to make the presence of the hyphen optional as a number could be positive as well.
  • \\d+ searches for one or more digits in the string.
  • (\\.\\d+)? is used to match numbers with decimal digits. \\. searches for the decimal point and \\d+ looks for one or more digits after the decimal point. The question mark at the end denotes that the entire regex part inside the parenthesis is optional as a number may not be a decimal.

We also need to make sure that the string is not null to avoid a NullPointerException.

public static boolean isStringNumeric(String number)
{
	boolean isNumeric;
	String regex = "-?\\d+(\\.\\d+)?";		
	if(number == null)
		isNumeric = false;
	else if(number.matches(regex))
		isNumeric = true;
	else
		isNumeric = false;		
	return isNumeric;
}
public static void main(String[] args)
{
	String num1 = "1001";
	String num2 = "-101";
	String num3 = "1a10";
	String num4 = null;		
	System.out.println("String " + num1 + " is numeric: " + isStringNumeric(num1));
	System.out.println("String " + num2 + " is numeric: " + isStringNumeric(num2));
	System.out.println("String " + num3 + " is numeric: " + isStringNumeric(num3));
	System.out.println("String " + num4 + " is numeric: " + isStringNumeric(num4));	
}


String 1001 is numeric: true
String -101 is numeric: true
String 1a10 is numeric: false
String null is numeric: false

Check if a String is Numeric by using Apache Commons Library

The Apache Commons library provides a few different methods that can be directly used to check whether a string is numeric or not. Let's look at each one of them separately.

NumberUtils.isCreatable() Method

The isCreatable() is a simple and convenient method that can be used 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));
}


String a10c is numeric: false
String -104 is numeric: true
String 100 is numeric: true
String 0xA10 is numeric: true

NumberUtils.isParsable() Method

As discussed in the previous section, if a string is parsable then it is a numeric string. The isParsable() method is used to check whether a string is parsable or not. It cannot work with hexadecimal numbers or scientific notations like the 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));
}


String a10c is numeric: false
String -104 is numeric: true
String 100 is numeric: true
String 0xA10 is numeric: false

StringUtils.isNumeric() Method

The isNumeric() method can also be used but it is a little less flexible than the other methods. It will only check for Unicode digits and will return false if the numeric string denotes a negative number or contains a decimal point. So 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));
}


String a10c is numeric: false
String -104 is numeric: false
String 100 is numeric: true
String 0.11 is numeric: false

StringUtils.isNumericSpace() Method

The isNumericSpace() is similar to the isNumeric() method. The only addition is that the isNumericSpace() 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.

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));
}


String a10c is numeric: false
String is numeric: false
String 100 is numeric: true
String 0.11 is numeric: false

Summary

There are multiple ways of checking whether a string is numeric or not. We can parse the string and if we don't get a NumberFormatException then it is a numeric string. We can also use regular expressions and the matches() method to check for numeric strings. The Apache Commons Library also provides a few methods to check numeric strings.



About the author:
I am a 3rd-year Computer Science Engineering student at Vellore Institute of Technology. I like to play around with new technologies and love to code.