Checking if a String Is Numeric

JavaJavaBeginner
Practice Now

Introduction

In Java, a string is a frequently used data type that can be used for various tasks. It is often used to store input from a text field or text area. It is important to determine whether the input from these sources is valid numeric data. In this lab, we will learn different ways to check if a string is a valid number in Java.

Using Parsing Method to Check Numeric Strings

Parsing is the easiest and best method to determine whether a string is numeric in Java. Java provides different built-in parsing methods, such as Integer.parseInt(String number), Double.parseDouble(String number), Float.parseFloat(String number), and Long.parseLong(String number), acting as the corresponding number data type converter.

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

To run the code:

  • Save the code in a file named NumericStringParser.java
  • Open the terminal and navigate to the directory containing the file
  • Compile the code using javac NumericStringParser.java
  • Run the code using java NumericStringParser

Using Regular Expression to Check Numeric Strings

Regular expressions can be used to match a string and determine whether it is numeric or not. We use the matches() method of strings to compare the string number with regular expressions. To match strings, the regex "-?\\d+(\\.\\d+)?" is used, where -? denotes whether the number is negative or not, \\d+ searches for one or more digits in the string, and (\\.\\d+)? is used to match decimal numbers. Here, the \\. searches for the decimal point, and \\d+ looks for one or more digits after the decimal point.

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

To run the code:

  • Save the code in a file named NumericStringRegex.java
  • Open the terminal and navigate to the directory containing the file
  • Compile the code using javac NumericStringRegex.java
  • Run the code using java NumericStringRegex

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

Summary

In this lab, we learned multiple ways of checking whether a string is numeric in Java and how to use them. We used the built-in parsing method, regular expressions, and Apache commons library to check whether a string is numeric or not in Java.

Other Java Tutorials you may like