Java isNumeric method

In this post, we will see how to implement isNumeric method in java.

There are many way to check if String is numeric or not.

Let’s see one by one.


Using regular expressions

You can use below regular expression to check if string is numeric or not.

[-+]?\\d*\\.?\\d+

Output:

223 is numeric : true
27.8 is numeric : true
4.2d is numeric : false
abc is numeric : false
-123 is numeric : true

Using Apache Commons library

You need to add below dependency to get Apache commons jar.

NumberUtils.isCreatable(String)

You can NumberUtils.isCreatable(String) to check if String is numeric or not.

This method accepts:

  • Scientific notation (for example 2.9e-10)
  • Hexadecimal numbers with prefix 0x or 0X
  • Octal numbers starting with prefix 0
  • Numbers marked with a type qualifier (for example 20L or 11.2d)

Output:

223 is numeric : true
27.8 is numeric : true
4.2d is numeric : true
abc is numeric : false
-123 is numeric : true

StringUtils.isNumeric(CharSequence)

StringUtils.isNumeric(CharSequence) checks if the CharSequence contains only Unicode digits.

This method can accept unicode digits in any language.

It can not accept positive or negative signs or decimal points.

Output:

223 is numeric : true
27.8 is numeric : false
4.2d is numeric : false
abc is numeric : false
-123 is numeric : false

NumberUtils.isDigits(String)

NumberUtils.isDigits(String) checks if the String contains only digits.

Output:

223 is numeric : true
27.8 is numeric : false
4.2d is numeric : false
abc is numeric : false
-123 is numeric : false

Using simple java code

You can simply use parseXXX() to check if String is numeric or not.

For example:
You can use Double.parseDouble() method to check if String is double or not.

Output:

223 is numeric :true
27.8 is numeric : true
4.2d is numeric : true
abc is numeric : false
-123 is numeric : true

Although this is not considered as good idea to check String with try and catch due to unnecessary overhead.

That’s about Java isNumeric method.

Related Posts

  • 13 July

    Remove substring from String in Java

    Table of ContentsUsing String’s replace method to remove substring from String in JavaUsing String’s replaceFirst method to remove substring from String in JavaUsing String’s replaceAll method to remove substring from String in JavaUsing StringBuilder’s delete() method to remove substring from String in JavaUsing StringBuilder’s replace() method to remove substring from String in JavaConclusion In this […]

  • Java String contains IgnoreCase
    23 June

    Java String contains Ignore Case

    Table of ContentsUsing String.toLowerCase()Using Pattern.compile()Using String’s MatchesUsing StringUtils.containsIgnoreCase()Complete program for Java String contains IgnoreCaseConclusion We often have to check if a string contains a substring or not in a case-insensitive manner. There are multiple ways to do so. In this article, we will take a look at these methods for java String contains Ignore Case […]

  • Find and count occurences of substring in String in Java
    02 June

    Find and count occurrences of substring in string in java

    Table of ContentsUsing the indexOf() methodUsing regular expressionUsing split() methodUsing the countMatches() method from Apache Commons library In this post, we will see how to find and count occurrences of substring in string in java. Using the indexOf() method The indexOf() method in java is a specialized function to find the index of the first […]

  • Print double quotes in java
    23 May

    Print double quotes in java

    Table of ContentsPrint double quotes in java using backslash characterPrint double quotes in java using unicode characterPrint double quotes in java by appending double quote character 💡 Outline You can print double quotes in java by escape double quotes using backslash character(\). [crayon-61633441723a5487343733/] When you print on console using System.out.println, you use double quotes and […]

  • Java Split String by Space
    13 May

    Java split string by space

    Learn about how to split String in java in different ways.

  • Split String by new line in java
    09 May

    How to split String by newline in java

    Learn about how to split String by newline in java

Comments

  1. NumberUtils.isCreatable(String) will really be helpful for actual numbers validation/coverage. Great post of covering different number validation options. Keep up the good work!

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.