Verify A String Contains Only Numeric Value Using Java

Introduction

The below example shows how to verify that a string contains only numeric value in Java. String may contain float value, double value etc. This example uses regular expression to check a string contains only numeric values. It makes sure that your string should contain only digits and dot(.).

You may need sometimes to use string variable for storing numeric data. As the variable is string type, so by mistake your string variable may contain non-numeric values which are not correct values for processing further business logic. Therefore you need to verify the value in string before processing it for your business requirements.

Here I will create a method for validating a given string for numeric values and this method will not throw any exception if validation fails.

Prerequisites

Java 1.6+

Numeric Validation

Below is the method which validates a string contains only numeric values. The following method returns false if comma and currency ($) are found in the string.

public static boolean isStringNumeric(String str) {
	final String Digits = "(\\p{Digit}+)";
	final String HexDigits = "(\\p{XDigit}+)";
	final String Expression = "[eE][+-]?" + Digits;
	final String numericRegex = "[\\x00-\\x20]*[+-]?(NaN|Infinity|(((" + Digits + "(\\.)?(" + Digits + "?)("
			+ Expression + ")?)|(\\.(" + Digits + ")(" + Expression + ")?)|(((0[xX]" + HexDigits + "(\\.)?)|(0[xX]"
			+ HexDigits + "?(\\.)" + HexDigits + ")" + ")[pP][+-]?" + Digits + "))[fFdD]?))[\\x00-\\x20]*";
	final Pattern NUMBER_PATTERN = Pattern.compile(numericRegex);
	return NUMBER_PATTERN.matcher(str).matches();
}

If you think that a comma separated number is also valid then you can use the following code snippets:

public static boolean isStringNumericWithComma(String str) {
	final String Digits = "(\\p{Digit}+)";
	final String HexDigits = "(\\p{XDigit}+)";
	final String Expression = "[eE][+-]?" + Digits;

	final String numericRegex = "[\\x00-\\x20]*[+-]?[\\d\\.,]*(NaN|Infinity|(((" + Digits + "(\\.)?(" + Digits + "?)("
			+ Expression + ")?)|(\\.(" + Digits + ")(" + Expression + ")?)|(((0[xX]" + HexDigits + "(\\.)?)|(0[xX]"
			+ HexDigits + "?(\\.)" + HexDigits + ")" + ")[pP][+-]?" + Digits + "))[fFdD]?))[\\x00-\\x20]*";

	final Pattern NUMBER_PATTERN = Pattern.compile(numericRegex);

	return NUMBER_PATTERN.matcher(str).matches();
}

Even if you think that a currency symbol ($) in the numeric value is also valid then you can use the following code snippet:

public static boolean isStringNumericWithCommaCurrency(String str) {
	final String Digits = "(\\p{Digit}+)";
	final String HexDigits = "(\\p{XDigit}+)";
	final String Expression = "[eE][+-]?" + Digits;

	final String numericRegex = "^[\\$]?[\\x00-\\x20]*[+-]?[\\d\\.,]*(NaN|Infinity|(((" + Digits + "(\\.)?(" + Digits + "?)("
			+ Expression + ")?)|(\\.(" + Digits + ")(" + Expression + ")?)|(((0[xX]" + HexDigits + "(\\.)?)|(0[xX]"
			+ HexDigits + "?(\\.)" + HexDigits + ")" + ")[pP][+-]?" + Digits + "))[fFdD]?))[\\x00-\\x20]*";

	final Pattern NUMBER_PATTERN = Pattern.compile(numericRegex);

	return NUMBER_PATTERN.matcher(str).matches();
}

Main Method

Below is the test method which test the input value using the above methods.

public static void main(String[] args) {
	System.out.println("=====================================");
	System.out.println("Comma and currency are invalid");
	System.out.println("=====================================");
	System.out.println("20,00: " + isStringNumericWithComma("20,00"));
	System.out.println("30,000.01: " + isStringNumericWithComma("30,000.01"));
	System.out.println("abc: " + isStringNumeric("abc"));
	System.out.println("abc125: " + isStringNumeric("abc125"));
	System.out.println("abc125.25: " + isStringNumeric("abc125.25"));
	System.out.println("125.25d: " + isStringNumeric("125.25d"));
	System.out.println("125d: " + isStringNumeric("125d"));
	System.out.println("125f: " + isStringNumeric("125f"));
	System.out.println("125.00: " + isStringNumeric("125.00"));
	System.out.println("125: " + isStringNumeric("125"));
	System.out.println("20: " + isStringNumeric("20"));
	System.out.println("20,00: " + isStringNumeric("20,00"));
	System.out.println("30.01: " + isStringNumeric("30.01"));
	System.out.println("30,000.01: " + isStringNumeric("30,000.01"));
	System.out.println("-2980: " + isStringNumeric("-2980"));
	System.out.println("$20: " + isStringNumeric("$20"));
	System.out.println("jdl: " + isStringNumeric("jdl"));
	System.out.println("2lk0: " + isStringNumeric("2lk0"));
	
	System.out.println("=====================================");
	System.out.println("Comma is valid");
	System.out.println("=====================================");
	System.out.println("20,00: " + isStringNumericWithComma("20,00"));
	System.out.println("30,000.01: " + isStringNumericWithComma("30,000.01"));
	
	System.out.println("=====================================");
	System.out.println("Comma and currency are valid");
	System.out.println("=====================================");
	System.out.println("20,00: " + isStringNumericWithCommaCurrency("$20,00"));
	System.out.println("30,000.01: " + isStringNumericWithCommaCurrency("$30,000.01"));
	System.out.println("$20: " + isStringNumericWithCommaCurrency("$20"));
}

Testing Number Validation

Running the above main method will give you the following output:

=====================================
Comma and currency are invalid
=====================================
20,00: true
30,000.01: true
abc: false
abc125: false
abc125.25: false
125.25d: true
125d: true
125f: true
125.00: true
125: true
20: true
20,00: false
30.01: true
30,000.01: false
-2980: true
$20: false
jdl: false
2lk0: false
=====================================
Comma is valid
=====================================
20,00: true
30,000.01: true
=====================================
Comma and currency are valid
=====================================
20,00: true
30,000.01: true
$20: true

That’s all about validating a number using Java.

Source Code

Download

Leave a Reply

Your email address will not be published. Required fields are marked *