1.用Java自带方法Character.isDigit()
public static boolean isNumericZidai(String str) {
for (int i = 0; i < str.length(); i++) {
System.out.println(str.charAt(i));
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
}
2、正则表达式
Boolean strResult = str.matches("-?[0-9]+.?[0-9]*");
if(strResult == true) {
System.out.println("Is Number!");
} else {
System.out.println("Is not Number!");
}
3.ASCII码
public static boolean isNumeric(String str){
for(int i=str.length();--i>=0;){
int chr=str.charAt(i);
if(chr<48 || chr>57)
return false;
}
return true;
}