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.

💻 Awesome Tech Resources:
  • Looking for ⚒️ tech jobs? Go to our job portal.
  • Looking for tech events? Go to tech events 🗓️ Calendar.️

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

  • 15 April

    How to declare a String array in java

    Learn about how to declare String array in java.

  • Escape double quotes in String in Java
    19 January

    How to escape double quotes in String in java

    In this post, we will see how to escape double quotes in String in java. There are scenarios where you need to escape double quotes already present in the String. This generally happens while dealing with JSON file format or reading file data. Escape double quotes in java Double quotes characters can be escaped with […]

  • 24 December

    How to capitalize first letter in java

    Learn about how to capitalize first letter in java.

  • 06 December

    Difference between replace() and replaceAll() in java

    In this post, we will see difference between String’s replace() and replaceAll() methods in java. String’s replace() and replaceAll() both replace all occurences in the String. String’s replace() takes either two chars or two CharSequences as arguments and it will replace all occurrences of char or String but replaceAll() method takes regex String as argument […]

  • 24 April

    Add character to String in java

    In this post, we will see how to add character to String in java. There are multiple ways to add character to String. 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for tech events? Go to tech events 🗓️ Calendar.️ Add character to the start of String You […]

  • 24 March

    Initialize List of String in java

    In this post, we will see how to initialize List of String in java. Can you initialize List of String as below: [crayon-607a493e493b5231555949/] You can't because List is an interface and it can not be instantiated with new List(). 💻 Awesome Tech Resources: Looking for ⚒️ tech jobs? Go to our job portal. Looking for […]

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.