You could have a function that tries to parse the string into an Integer or a Double and return true if it succeeded, or return false is an exception was thrown during parsing. Parsing into a Double should be enough since all integer values are decimal values without the .0
public static boolean isNumber(String s) {
try {
Double.parseDouble(s);
/* Use Integer.parseInt(s) instead, if
you want to check if the String s
is an Integer */
} catch(NumberFormatException e) { // string is not a number
return false;
}
return true;
}
Then you can say if(!isNumber(str)) to check if the String str is not a number.
Alternatively, you could make the isNumber() be a isNotNumber() by swapping the return false and return true statements.
If you don't want to use exceptions, a different approach would be the following. Since we know a valid number can only contain digits and at most 1 dot for decimal point, we can iterate through the string and check for each character:
- if it is not a digit and not a dot, return false
- if it is a dot but a dot was already found, return false
- otherwise it is valid number character and we do nothing
Here is a sample function:
public static boolean isNumber(String s) {
int dotCount = 0;
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) != '.' && !Character.isDigit(s.charAt(i))) {
return false;
} else if(s.charAt(i) == '.') {
if(dotCount == 1) {
return false;
}
dotCount = 1;
}
}
return true;
}
EDIT: based on @MadProgrammer's suggestions:
A more general approach that will accept values separated with commas such as 1,35 or any amount of spaces within the number string like with 123 456 . 333.
Approach:
Iterate through the string and check for each character:
- if it is not a digit, dot, comma, or a space, return false
- if it is a dot or a comma but one of them was already found, return false
- otherwise it is valid number character and we do nothing
So the code would look something like:
public static boolean isNumber(String s) {
int separatorCount = 0; // count dots and commas
char currChar;
s.trim(); // remove trailing and leading whitespace
for (int i = 0; i < s.length(); i++) {
currChar = s.charAt(i);
if (currChar != '.' && currChar != ',' && currChar != ' '
&& !Character.isDigit(currChar)) {
return false;
} else if (currChar == '.' || currChar == ',') {
if (separatorCount == 1) {
return false;
}
separatorCount = 1;
}
}
return true;
}
Another solution could use the NumberFormat's parse() method. However, this method only checks the beginning of the string (for example, for 12.3.3 it will return 12.3) so we have to return false if the returned string doesn't equal the input string as well as if the ParseException is thrown.
public static boolean isNumber(String s) {
try {
String newVal = NumberFormat.getInstance().parse(s).toString();
if (!newVal.equals(s)) {
return false;
}
} catch (ParseException e) {
return false;
}
return true;
}
NOTE: All of the methods should probably have a check if(s == null) { return false; } for the input String s to prevent a NullPointerException