Check if String is number in Java example shows how to check if the string is a number. It also shows how to check if the string is a number using the regex or Double class.
How to check if the string is a number in Java?
There are a couple of ways using which we can check that. You can check this by using the parseDouble
method of the Double class. This method parses the input string to a double value and returns it if the string contains a valid number. However, it throws NumberFormatException if the input string contains an invalid numeric value.
We will make use of this simple function to check if the string contains a valid number. Here is the example code that has a method that makes use of that.
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
package com.javacodeexamples.stringexamples; public class CheckStringIsNumber { public static void main(String[] args) { String[] strNumbers = { "12", "1000", "10.0", "-59", "100.23", "0.5", "-0.0", "acd12", "12.2.3" }; for(String strNumber : strNumbers) { System.out.println( strNumber + " => " + isNumber(strNumber) ); } } private static boolean isNumber(String str) { boolean blnNumber = false; try { /* * This will throw NumberFormatException if * the string does not have a valid number */ Double.parseDouble(str); //set it to true blnNumber = true; }catch(NumberFormatException nfe) { //do nothing } return blnNumber; } } |
Output
1 2 3 4 5 6 7 8 9 |
12 => true 1000 => true 10.0 => true -59 => true 100.23 => true 0.5 => true -0.0 => true acd12 => false 12.2.3 => false |
As you can see from the output, the method correctly identifies the valid integers, double values, and negative numeric values.
The only problem with this approach is the performance. If you have a very large number of values that need to be checked using this approach, you might suffer some efficiency.
How to check if the string contains a valid number using the regex pattern?
We can do the same check using a regex pattern as well. Here is the pattern that we are going to use.
1 |
^(-)?\\d+(\\.\\d+)?$ |
Where,
1 2 3 4 5 |
^ - Start of the string (-)? - Minus sign zero or 1 time \\d+ - Any digit between 0 to 9 one or more times (\\.\\d+)? - Group of a dot and one or more digits zero or 1 time $ - End of the string |
Let’s implement this pattern and check the same values again.
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 com.javacodeexamples.stringexamples; public class CheckStringIsNumber { public static void main(String[] args) { String[] strNumbers = { "12", "1000", "10.0", "-59", "100.23", "0.5", "-0.0", "acd12", "12.2.3" }; for(String strNumber : strNumbers) { System.out.println( strNumber + " => " + isNumber(strNumber) ); } } private static boolean isNumber(String str) { if(str == null) return false; else return str.matches("^(-)?\\d+(\\.\\d+)?$"); } } |
Output
1 2 3 4 5 6 7 8 9 |
12 => true 1000 => true 10.0 => true -59 => true 100.23 => true 0.5 => true -0.0 => true acd12 => false 12.2.3 => false |
As you can see from the output, the regex pattern correctly identified the valid numbers.
If you want to learn more about the regex, please visit the Java Regex tutorial. Also, please visit the Java String tutorial to learn more about string as well.
Please let me know your views in the comments section below.