How To Check If A String Is Numeric In Java

This example show you how to check if a string content is numeric or not. It provide totally six methods to use.

package com.dev2qa.calculatorexample;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by zhaosong on 2018/5/23.
 */

public class CheckStringNumeric {

    public static void main(String args[])
    {
        CheckStringNumeric checkStringNumeric = new CheckStringNumeric();

        checkStringNumeric.isNumeric("-123.45");

        checkStringNumeric.isInteger("-123");

        checkStringNumeric.isIntegerByParse("-123.456");

        checkStringNumeric.isDoubleByParse("-123.456789");

        checkStringNumeric.isFloatByParse("-123.11111");

        checkStringNumeric.isBooleanByParse("true");

    }


    /* Use Character class's isDigit method to check each char in string. */
    public boolean isNumeric(String value) {

        boolean ret = true;

        if (value != null) {
            value = value.trim();

            // Get total char number.
            int length = value.length();

            // Record total decimal point number in this string.
            int decimalPointCount = 0;

            for (int i = 0; i < length; i++) {

                // Get each char.
                char c = value.charAt(i);

                // Check whether the char is a digit or not.
                if (!Character.isDigit(c)) {

                    if(i==0)
                    {
                        // Means positive or negative number.
                        if(c=='+' || c=='-')
                        {
                            continue;
                        }
                    }else
                    {
                        // Means the decimal point.
                        if(c=='.')
                        {
                            decimalPointCount++;

                            // If there are two or more decimal point in the string then it is not a number.
                            if(decimalPointCount > 1)
                            {
                                ret = false;
                                break;
                            }else {
                                continue;
                            }
                        }
                    }

                    ret = false;
                    break;
                }
            }
        }


        if(ret)
        {

            System.out.println(value + " is a number.");
        }else
        {
            System.out.println(value + " is not a number.");
        }

        return ret;
    }

    /* Check integer value by regular expression string. */
    public boolean isInteger(String value) {

        String regularExpression = "^[-\\+]?[\\d]*$";

        Pattern pattern = Pattern.compile(regularExpression);

        Matcher matcher = pattern.matcher(value);

        boolean ret = matcher.matches();

        if(ret)
        {
            System.out.println(value + " is an integer.");
        }else
        {
            System.out.println(value + " is not an integer.");
        }

        return ret;
    }

    /* Check string integer value by Integer class. */
    public boolean isIntegerByParse(String value){

        boolean ret = true;

        try {

            if (value != null) {

                Integer.parseInt(value);
            }
        }catch(NumberFormatException ex)
        {
            // If parseInt() method throw NumberFormatException then it is not an integer.
            ret = false;
        }finally {

            if(ret)
            {
                System.out.println(value + " is an integer.");
            }else
            {
                System.out.println(value + " is not an integer.");
            }

            return ret;
        }
    }

    /* Check whether the string value is a double value or not by Double.parseDouble() method. */
    public boolean isDoubleByParse(String value){

        boolean ret = true;

        try {

            if (value != null) {

                Double.parseDouble(value);
            }
        }catch(NumberFormatException ex)
        {
            ret = false;
        }finally {

            if(ret)
            {
                System.out.println(value + " is a double number.");
            }else
            {
                System.out.println(value + " is not a double number.");
            }

            return ret;
        }
    }

    /* Check whether the string value is a float value or not by Float.parseFloat() method. */
    public boolean isFloatByParse(String value){

        boolean ret = true;

        try {
            if (value != null) {

                Float.parseFloat(value);
            }
        }catch(NumberFormatException ex)
        {
            ret = false;
        }finally {

            if(ret)
            {
                System.out.println(value + " is a float number.");
            }else
            {
                System.out.println(value + " is not a float number.");
            }

            return ret;
        }
    }

    /* Check whether the string value is a boolean value or not by Boolean.parseBoolean() method. */
    public boolean isBooleanByParse(String value){

        boolean ret = true;

        try {
            if (value != null) {

                Boolean.parseBoolean(value);
            }
        }catch(NumberFormatException ex)
        {
            ret = false;
        }finally {

            if(ret)
            {
                System.out.println(value + " is a boolean value.");
            }else
            {
                System.out.println(value + " is not a boolean value.");
            }

            return ret;
        }
    }
}

The output in console log is as below.

-123.45 is a number.
-123 is an integer.
-123.456 is not an integer.
-123.456789 is a double number.
-123.11111 is a float number.
true is a boolean value.
(Visited 57 times, 1 visits today)
READ :   How To Connect Localhost Apache James With Thunderbird

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.