如题。谢谢各位!~

解决方案 »

  1.   

    kingfish(龙城) :能给个实例吗?谢谢你哈!~
      

  2.   

    用正则表达式
    Pattern p=Pattern.compile("^0|(-?[1-9]\\d*$");//这是整数的正则表达式
    String s="12343";
    if(p.matcher(s).matches()){//整数
      BigInteger b=new BigInteger(s);
      if(b.compareTo(new BigInteger(Integer.MIN_VALUE)>0&&
          b.compareTo(new BigInteger(Integer.MAX_VALUE)){//在int的范围内
        int k=b.intValue();//得到这个整数
      }
    }double同上,正则表达式为^0|(\\x2E\\d+)|([+-]?0\\x2E\\d+)|([+-]?[1-9]\\d*(\\x2E\\d+)?)$
      

  3.   

    public static boolean estimate(String s)
    {
           Pattern p = Pattern
              .compile("^0|(\\x2E\\d+)|([+-]?0\\x2E\\d+)|([+-]?[1-9]\\d*(\\x2E\\d+)?)$");
    if (p.matcher(s).matches())
    {
    BigInteger b = new BigInteger(s);
    a = b.compareTo(new BigInteger(String.valueOf(Double.MIN_VALUE))) > 0
        && b.compareTo(new BigInteger(String.valueOf(Double.MAX_VALUE))) < 0;
    }
    Integer变成Double就不行了,但是变成Long又可以了,这是怎么回事啊?谢谢? return a;
    }
      

  4.   

    public class StringIdentifier {
        public static boolean isInteger(String sample){
            char[] charArray=sample.toCharArray();
            int index=0;
            if(charArray[0]=='-')
                index=1;
            for(;index<charArray.length;index++)
                if(!Character.isDigit(charArray[index]))
                    return false;
            return true;
        }
        public static boolean isFloat(String sample){
            int dot=0;
            char[] charArray=sample.toCharArray();
            int index=0;
            if(charArray[0]=='-')
                index=1;
            for(;index<charArray.length;index++){
                if(!Character.isDigit(charArray[index])&&charArray[index]!='.'){
                    return false;
                }
                if(charArray[index]=='.')
                    dot++;
            }
            if(dot<2)
                return true;
            else
                return false;
        }
        public static void main(String[] args){
            String sample="-123";
            if(StringIdentifier.isInteger(sample))
                System.out.println(Integer.valueOf(sample));
            else
                System.out.println("cannot cast \""+sample+"\" into an Integer");
            if(StringIdentifier.isFloat(sample))
                System.out.println(Float.valueOf(sample));
            else
                System.out.println("cannot cast \""+sample+"\" into a Float");
        }
    }
      

  5.   

    用我这个笨方法,或者用正则表达式。
    千万别用异常判断,会影响JVM运行效率(参见Effective Java)。