To check if a string is a number in java we have different methods . The easiest way using plain java Now we will see methods to check a given string value is number or not in java .
1. Check if String is Numeric with Core Java
In this method we will see way for checking string is a integer or not .
- Integer.parseInt()
- Integer.valueOf()
- Double.parseDouble()
- Float.parseFloat()
- Long.parseLong()
These method convert string to equivalent number otherwise throw NumberFormatException . If String is convert to number successfully , it means String contains number otherwise not .Let’s see example
public class CheckStingIsNumeric {
public static void main(String[] args) {
String string = "23451.35";
boolean isNumeric = true;
try {
Double num = Double.parseDouble(string);
} catch (NumberFormatException e) {
isNumeric = false;
}
if(isNumeric)
System.out.println(string + " is a number");
else
System.out.println(string + " is not a number");
}
}
Output:
23451.35 is a number
2. Check if a string is numeric or not using regular expressions (regex)
In this method we will use regex for check if string is number in java
public class StringIsNumber {
public static void main(String[] args) {
String string = "-452.15";
boolean isNumeric = true;
isNumeric = string.matches("-?\\d+(\\.\\d+)?");
if(isNumeric)
System.out.println(string + " is a number");
else
System.out.println(string + " is not a number");
}
}
Output :
-452.15 is a number
3. Check if String is Numeric with Apache Commons
Apache common is most popular third party library for expanding basic java operations. We will be looking two classes StringUtils and NumberUtils of apache common . Firstly you need to add apache common dependency in pom file if you are using maven project .If you are using java project , then you can add apache common jar in path. There is apche common dependency .
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
Add this dependency in pom.xml . Let’s see code for this.
NumberUtils.isParsable() –
IsParsable(String str) method use for Checks whether the given String is a parsable number. Parsable numbers include those Strings understood by Integer.parseInt(String), Long.parseLong(String), Float.parseFloat(String) or Double.parseDouble(String). isParsable() method can be used instead of catching ParseException when calling one of those methods. Hexadecimal and scientific notations are not considered parsable. See isCreatable(String) on those cases. Null and empty String will return false.
Syntax :
boolean NumberUtils.isParsable(String str){
}
Parameters: str – the String to check.
Return : true if the string is a parsable number.
String string = "10.10";
if (NumberUtils.isParsable(string)) {
System.out.println("String is numeric!");
} else {
System.out.println("String is not numeric.");
}
Output :
String is numeric!
NumberUtils.isCreatable()-
NumberUtils.isCreatable() method checks whether the String a valid Java number. Valid numbers include hexadecimal marked with the 0x or 0X qualifier, scientific notation , octal numbers and numbers marked with a type qualifier (e.g. 123L). Non-hexadecimal strings beginning with a leading zero are treated as octal values . Thus the given string 09 will return false, since 9 is not a valid octal value. However, numbers beginning with 0. are treated as decimal. null and empty/blank String will return false.
Syntax :
public static boolean isCreatable(String str){
}
Parameters: str – the String to check
Returns: true if the string is a correctly formatted number
String string = "0x20AD";
if (NumberUtils.isCreatable(string)) {
System.out.println("String contains a creatable number!");
} else {
System.out.println("String doesn't contain creatable number.");
}
Output :
String contains a creatable number!
NumberUtils.isDigits() –
Checks whether the String contains only digit characters. Null and empty String will return false.
Syntax :
public static boolean isDigits(String str){
}
Parameters: str – the String to check
Returns: true if str contains only Unicode numeric
String string = "100";
if (NumberUtils.isDigits(string)) {
System.out.println("String is numeric!");
} else {
System.out.println("String isn't numeric.");
}
Output :
String is numeric!
StringUtils.isNumeric()
StringUtils.isNumeric() method checks if the CharSequence contains only Unicode digits. A decimal point is not a Unicode digit and returns false. null will return false. An empty CharSequence (length()=0) will return false.
Synstax :
public static boolean isNumeric(CharSequence cs){
}
Parameters: cs – the CharSequence to check, may be null
Returns: true if only contains digits, and is non-null
String string = "100";
if (StringUtils.isNumeric(string)) {
System.out.println("String is numeric!");
} else {
System.out.println("String isn't numeric.");
}
Output :
String is numeric!
StringUtils.isNumericSpace()
Checks if the CharSequence contains only Unicode digits or space (‘ ‘). A decimal point is not a Unicode digit and returns false. null will return false. An empty CharSequence (length()=0) will return true.
public static boolean isNumericSpace(CharSequence cs){
}
Parameters: cs – the CharSequence to check, may be null
Returns: true if only contains digits or space, and is non-null
String string = "25 50 15";
if (StringUtils.isNumericSpace(string)) {
System.out.println("String is numeric!");
} else {
System.out.println("String isn't numeric.");
}
Output :
String is numeric!
Reference :