xxxxxxxxxx
public boolean isNumeric(String str) {
return str.matches("-?\\d+(\\.\\d+)?");
}
xxxxxxxxxx
String someString = "123123";
boolean isNumeric = someString.chars().allMatch( Character::isDigit );
xxxxxxxxxx
private boolean isInt(String s){
try{
Integer.parseInt(s);
return true;
}catch (NumberFormatException e){
return false;
}
}
xxxxxxxxxx
String value = 12345
StringUtils.isNumeric(value) //returns true
xxxxxxxxxx
public static boolean isNumeric(String strNum) {
if (strNum == null) {
return false;
}
try {
double d = Double.parseDouble(strNum);
} catch (NumberFormatException nfe) {
return false;
}
return true;
}
xxxxxxxxxx
public static boolean isNumeric(String str) {
try {
Double.parseDouble(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}
public static void main(String[] args) {
String input = "123.45";
if (isNumeric(input)) {
System.out.println("The string is a number.");
} else {
System.out.println("The string is not a number.");
}
}