Java判斷輸入是否為數字-jsp入門教程
Java判斷輸入是否為數字-jsp教程入門教程
public class CharTest {
public CharTest() {
}
public static void main(String args[]) {
String s = "123abc";
for (int i=0;i<s.length();i++) {
char ch = s.charAt(i);
System.out.println(ch + " is digit? " + Character.isDigit(ch));
System.out.println(ch + " is digit? " + (ch >= '0' && ch <= '9'));
}
}
}
方法二
if(str==null || "".equals(str.trim())){
return true;
}
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str.trim());
if(isNum.matches()){
System.out.println("是一個數字");
}else{
System.out.println("不是一個數字");
}