当前位置:编程学习 > JSP >>

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("不是一个数字");
}

补充:Jsp教程,J2ME开发
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,