Determine if a String is a valid number in Java
This post will explore several ways to determine whether a string is a valid number in Java. The solution checks if a string contains only Unicode digits and doesn’t allow either positive or negative leading sign or a decimal point. A null string or an empty string should return false.
Java did not provide any standard method for this simple task. Nevertheless, there are several methods to check if the given string is numeric in Java:
1. Custom method
A simple solution is to write our own utility method for this simple task. The idea is to iterate over string characters and check each character to be numeric using Character.isDigit(char). This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Main { public static boolean isNumeric(String s) { if (s == null || s.equals("")) { return false; } for (int i = 0; i < s.length(); i++) { char c = s.charAt(i); if (c < '0' || c > '9') { return false; } } return true; } public static void main(String[] args) { String s = "100"; System.out.println("IsNumeric: " + isNumeric(s)); } } |
Output:
IsNumeric: true
2. Using Lambda Expressions
From Java 8 onwards, this can be efficiently done using lambda expressions:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Main { public static boolean isNumeric(String s) { if (s == null || s.equals("")) { return false; } return s.chars().allMatch(Character::isDigit); } public static void main(String[] args) { String s = "100"; System.out.println("IsNumeric: " + isNumeric(s)); } } |
Output:
IsNumeric: true
3. Using Regular Expression
We can use the regular expression "[0-9]+" or "\\d+" which checks if a string is numeric. This can be done using the matches() method of the String class, which tells whether this string matches the given regular expression.
|
1 2 3 4 5 6 7 8 9 10 |
class Main { public static void main(String[] args) { String s = "100"; boolean isNumeric = (s != null && s.matches("[0-9]+")); System.out.println("IsNumeric: " + isNumeric); } } |
Output:
IsNumeric: true
If the regular expression is frequently called, we should compile it first for performance boost:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import java.util.regex.Pattern; class Main { private static Pattern p = Pattern.compile("[0-9]+"); public static void main(String[] args) { String s = "100"; boolean isNumeric = (s != null && p.matcher(s).find()); System.out.println("IsNumeric: " + isNumeric); } } |
Output:
IsNumeric: true
4. Using Apache Commons Lang
We can also use Apache Commons Lang library, which provides several methods to check for a numeric string:
1. StringUtils#isNumeric()
2. NumberUtils#isCreatable()
3. NumberUtils#isNumber()
|
1 2 3 4 5 6 7 8 9 10 |
import org.apache.commons.lang3.StringUtils; class Main { public static void main(String[] args) { String s = "100"; System.out.println("IsNumeric: " + StringUtils.isNumeric(s)); } } |
5. Using Wrapper classes built-in methods
The idea is to parse a string by Integer.parseInt() or Long.parseLong(), which generates a NumberFormatException for non-numeric strings. Notice that this method fails when the value is outside the range for int or long, respectively.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Main { public static boolean isNumeric(String s) { try { Integer.parseInt(s); } catch (NumberFormatException ex) { return false; } return true; } public static void main(String[] args) { String s = "100"; System.out.println("IsNumeric: " + isNumeric(s)); } } |
Output:
IsNumeric: true
That’s all about checking if a String is a valid number or not in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)