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.
Table of Contents
Using regular expressions
You can use below regular expression to check if string is numeric or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package org.arpit.java2blog; public class JavaisNumericMain { public static void main(String args[]) { System.out.println("223 is numeric :"+isNumeric("223")); System.out.println("27.8 is numeric : "+isNumeric("27.8")); System.out.println("4.2d is numeric : "+isNumeric("4.2d")); System.out.println("abc is numeric : "+isNumeric("abc")); System.out.println("-123 is numeric : "+isNumeric("-123")); } public static boolean isNumeric(String str) { return str != null && str.matches("[-+]?\\d*\\.?\\d+"); } } |
Output:
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.
1 2 3 4 5 6 7 |
<dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.9</version> </dependency> |
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import org.apache.commons.lang3.math.NumberUtils; public class JavaisNumericMain { public static void main(String args[]) { System.out.println("223 is numeric : "+NumberUtils.isCreatable("223")); System.out.println("27.8 is numeric : "+NumberUtils.isCreatable("27.8")); System.out.println("4.2d is numeric : "+NumberUtils.isCreatable("4.2d")); System.out.println("abc is numeric : "+NumberUtils.isCreatable("abc")); System.out.println("-123 is numeric : "+NumberUtils.isCreatable("-123")); } } |
Output:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import org.apache.commons.lang3.StringUtils; public class JavaisNumericMain { public static void main(String args[]) { System.out.println("223 is numeric : "+StringUtils.isNumeric("223")); System.out.println("27.8 is numeric : "+StringUtils.isNumeric("27.8")); System.out.println("4.2d is numeric : "+StringUtils.isNumeric("4.2d")); System.out.println("abc is numeric : "+StringUtils.isNumeric("abc")); System.out.println("-123 is numeric : "+StringUtils.isNumeric("-123")); } } |
Output:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package org.arpit.java2blog; import org.apache.commons.lang3.math.NumberUtils; public class JavaisNumericMain { public static void main(String args[]) { System.out.println("223 is numeric : "+NumberUtils.isDigits("223")); System.out.println("27.8 is numeric : "+NumberUtils.isDigits("27.8")); System.out.println("4.2d is numeric : "+NumberUtils.isDigits("4.2d")); System.out.println("abc is numeric : "+NumberUtils.isDigits("abc")); System.out.println("-123 is numeric : "+NumberUtils.isDigits("-123")); } } |
Output:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
package org.arpit.java2blog; public class JavaisNumericMain { public static void main(String args[]) { System.out.println("223 is numeric :"+isNumeric("223")); System.out.println("27.8 is numeric : "+isNumeric("27.8")); System.out.println("4.2d is numeric : "+isNumeric("4.2d")); System.out.println("abc is numeric : "+isNumeric("abc")); System.out.println("-123 is numeric : "+isNumeric("-123")); } public static boolean isNumeric(String str) { if (str == null || str.length() == 0) { return false; } try { Double.parseDouble(str); return true; } catch (NumberFormatException e) { return false; } } } |
Output:
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.
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!