如题

解决方案 »

  1.   

    需要确定一下“数字”是什么,是 byte? short? int? long? float? double? 或者是其他的大数字?每种数值类型的取值范围不一样,对于 byte 来说 256 这个数字就是不合法的!
      

  2.   

    就使用Integer的parseInt转化一下,有异常的就不是数字了。
      

  3.   

    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'));
         }
        }
    }不知道是不是你所要的
      

  4.   

    3L正解。好好看看thinking in Java
      

  5.   

    我并不认为 3 楼是正解,对于字符串 "12345678901234567890" 虽然全部都是数字字符,但是对于 int 来说它并不是一个数字。建议使用 2 楼的方法。
      

  6.   

    就像你在1楼说的,对不同的类型的数字,范围是不一样的,所以要先确定LS你所谓数字的范围,如果是整数范围内用In...可以,但是try catch终归不是什么好的办法,建议用4楼的说法,正则表达式
      

  7.   

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

  8.   

    直接在前台判断就可以了嘛。 <input type="text" id="num" name="num" onkeyup="this.value=this.value.replace(/\D/g,'')" />
      

  9.   

    为什么不用apache org.apache.commons.lang.StringUtils
    这个类中有许多实用的方法,其中有一个方法是isNumberic,可以很方便的判断。
      

  10.   

       不需要使用正则表达式,已有的方法很多的。可以去看jdk的api,比如Character,如果做过中文分词,就对这方面比较熟悉了。
      

  11.   

    Character类中的静态方法:
    public static boolean isDigit(char ch) {
            return isDigit((int)ch);
        }
      

  12.   


    StringUtils里面有很多实用的判断方法
      

  13.   

    先换转成long 来接受(用long 接受只是不确定他到底传多少进来)然后再用正则判断下 或则 就如2L那样 如果抛异常 你就catch