This Java example shows how to check if a string contains number using the Double class, regular expression, and apache commons library.
How to check if a string contains a number in Java?
1) Check if a string contains a number using the Double wrapper class
Use the parseDouble
method of the Double wrapper class to check. If the string does not contain a number, the parseDobule
method throws NumberFormatException
exception which you can catch to do further processing.
1 |
public static double parseDouble(String strNumber) throws NumberFormatException |
Example
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 |
package com.javacodeexamples.stringexamples; public class StringContainsNumberExample { public static void main(String[] args) { String[] strNumbers = {"123", "123a", "a123", "1.23", "0.230", "000000", "000-001", "", "+12.32", "-34.54"}; //loop through each number for(String strNumber : strNumbers){ try{ //try to parse the number Double.parseDouble(strNumber); //if the code reaches at this point, it means string indeed contained a number System.out.println("Valid: " + strNumber); //hmm, invalid number, hence the NumberFormatException }catch(NumberFormatException fe){ System.out.println("Invalid: " + strNumber); } } } } |
Output
1 2 3 4 5 6 7 8 9 10 |
Valid: 123 Invalid: 123a Invalid: a123 Valid: 1.23 Valid: 0.230 Valid: 000000 Invalid: 000-001 Invalid: Valid: +12.32 Valid: -34.54 |
2) Using Apache Commons Library
If you are using the Apache Commons library, you can use the isNumber
static method of the NumberUtils class to check if the string contains a valid number as given below.
1 |
public static boolean isNumber(String strNumber) |
This method returns true if the string contains a valid number, false otherwise.
1 2 3 4 5 6 7 8 9 10 11 12 |
String[] strNumbers = {"123", "123a", "a123", "1.23", "0.230", "000000", "000-001", "", "+12.32", "-34.54"}; //loop through each number for(String strNumber : strNumbers){ if( NumberUtils.isNumber(strNumber) ){ System.out.println("Valid: " + strNumber); }else System.out.println("Invalid: " + strNumber); } |
Output
1 2 3 4 5 6 7 8 9 10 |
Valid: 123 Invalid: 123a Invalid: a123 Valid: 1.23 Valid: 0.230 Valid: 000000 Invalid: 000-001 Invalid: Invalid: +12.32 Valid: -34.54 |
Note: As you may have observed, the isNumber
method returns false for “+12.32” value. Use this approach only if you do not expect such string in the input values.
3) Using a regular expression
You can also use a regular expression to check if the string contains a valid number or not as given below.
1 2 3 4 5 6 7 8 9 10 |
String regex = "\\d+"; //loop through each number for(String strNumber : strNumbers){ if( strNumber.matches(regex) ){ System.out.println("Valid: " + strNumber); }else System.out.println("Invalid: " + strNumber); } |
The “\\d” pattern denotes a digit in regular expression. Our pattern “\\d+” means “one or more digits”. You can also use the “[0-9]+” pattern instead of the “\\d+”.
Output
1 2 3 4 5 6 7 8 9 10 |
Valid: 123 Invalid: 123a Invalid: a123 Invalid: 1.23 Invalid: 0.230 Valid: 000000 Invalid: 000-001 Invalid: Invalid: +12.32 Invalid: -34.54 |
Note:
This pattern does not work for anything except for positive integers (and that too without the “+” sign). Use this only if it fits the requirements.
Let’s create a bit more accommodative pattern to include floating point and negative numbers.
Our original pattern was “\\d+” which checked only for one or more digits. Now we are going to include the minus sign “-” which should be optional, as well as the optional dot “.” for floating point numbers. Here is the updated regular expression pattern.
1 2 3 4 5 6 7 |
"-?\\d+(\\.\\d+)?" where -? Means zero or more minus sign "-" \\d+ followed by one or more digits (\\.\\d+) followed by zero or more dot "." and zero or more numbers |
Let’s test it out using the same input values
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
String[] strNumbers = {"123", "123a", "a123", "1.23", "0.230", "000000", "000-001", "", "+12.32", "-34.54"}; String regex = "-?\\d+(\\.\\d+)?"; //loop through each number for(String strNumber : strNumbers){ if( strNumber.matches(regex) ){ System.out.println("Valid: " + strNumber); }else System.out.println("Invalid: " + strNumber); } |
Output
1 2 3 4 5 6 7 8 9 10 |
Valid: 123 Invalid: 123a Invalid: a123 Valid: 1.23 Valid: 0.230 Valid: 000000 Invalid: 000-001 Invalid: Invalid: +12.32 Valid: -34.54 |
Looks like we have covered almost all the possible input values except for “+12.32” which gave us false while still being a valid number. We also want to add optional plus sign “+” at the beginning. Remember, a valid number contains either “-” or “+” sign. Both of them cannot appear together. Here is the final version of the pattern.
1 |
"(-|\\+)?\\d+(\\.\\d+)?" |
We have modified the first part from “-?” to “(-|\\+)?”. Basically we grouped them together and applied “or” operator “|” so that it looks for zero or more minus sign or plus sign.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
String[] strNumbers = {"123", "123a", "a123", "1.23", "0.230", "000000", "000-001", "", "+12.32", "-34.54"}; String regex = "(-|\\+)?\\d+(\\.\\d+)?"; //loop through each number for(String strNumber : strNumbers){ if( strNumber.matches(regex) ){ System.out.println("Valid: " + strNumber); }else System.out.println("Invalid: " + strNumber); } |
Output
1 2 3 4 5 6 7 8 9 10 |
Valid: 123 Invalid: 123a Invalid: a123 Valid: 1.23 Valid: 0.230 Valid: 000000 Invalid: 000-001 Invalid: Valid: +12.32 Valid: -34.54 |
This example is a part of the Java String tutorial.
Please let me know your views in the comments section below.