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.

Was this post helpful?

Related Posts

  • 28 October

    Convert UUID to String in Java

    Table of ContentsIntroductionUUID class in JavaConvert UUID to String in Java Introduction In this article, we will have a look on How to Convert UUID to String in Java. We will also shed some light on the concept of UUID, its use, and its corresponding representation in Java class. Let us have a quick look […]

  • 27 September

    Repeat String N times in Java

    Table of ContentsIntroductionWhat is a String in Java?Repeat String N times in JavaSimple For Loop to Repeat String N Times in JavaUsing Recursion to Repeat String N Times in JavaString.format() method to Repeat String N Times in JavaUsing String.repeat() method in Java 11 to Repeat String N TimesRegular Expression – Regex to Repeat String N […]

  • 15 September

    How to Replace Space with Underscore in Java

    Table of ContentsReplace space with underscore in java1. Using replace() method2. Using replaceAll() method Learn about how to replace space with underscore in java. Replace space with underscore in java 1. Using replace() method Use String’s replace() method to replace space with underscore in java. String’s replace() method returns a string replacing all the CharSequence […]

  • 15 September

    How to Replace Comma with Space in Java

    Table of ContentsReplace comma with space in java1. Using replace() method2. Using replaceAll() method Learn about how to replace comma with space in java. Replace comma with space in java 1. Using replace() method Use String’s replace() method to replace comma with space in java. Here is syntax of replace() method: [crayon-63da8d33c04d1493159040/] [crayon-63da8d33c04d9006392914/] Output: 1 […]

  • 11 June

    Remove Parentheses From String in Java

    Table of ContentsJava StringsRemove Parentheses From a String Using the replaceAll() MethodRemove Parentheses From a String by TraversingConclusion Java uses the Strings data structure to store the text data. This article discusses methods to remove parentheses from a String in Java. Java Strings Java Strings is a class that stores the text data at contiguous […]

  • Escape percent sign in java
    08 February

    Escape percent sign in String’s format method in java

    Table of ContentsEscape Percent Sign in String’s Format Method in JavaEscape Percent Sign in printf() Method in Java In this post, we will see how to escape Percent sign in String’s format() method in java. Escape Percent Sign in String’s Format Method in Java String’s format() method uses percent sign(%) as prefix of format specifier. […]

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.