谁能写个完成其对其字符进行判断是否为小数或整数的程序,谢谢!
例如:String aa="222.3";
String bb="sdf.342";
要写一个方法来判断上面两个字符是否为小数或整数的方法;谢谢,帮帮忙!我知道这个不难,我也写了一个如下:
public boolean getPan(String ss){
boolean bool=false;
//String regEx = "[\\.\\d]*";
try{
Pattern p=Pattern.compile("[\\.\\d]*");
Matcher m=p.matcher(ss);
boolean bo=m.find();
if (bo){ bool=true; }
return bool;
}catch(Exception e){
System.out.println("判断字符出错!"+e.getMessage());
return bool;
}
}
可是会出有出错提示:
java.lang.NoSuchMethodError 

解决方案 »

  1.   

    谁能写个完成其对其字符进行判断是否为小数或整数的程序,谢谢!
    --------String str="...";
    try {
    float f=Float.parseFloat(str);
    if(f%1.0==0)
       system.out.println("it's a integer");
    else 
       system.out.println("it's a float number");
    } catch(Exception e) {
    System.out.println("it's not a number");
    }
      

  2.   

    public static boolean getPan(String ss) {
    try {
    Pattern p = Pattern.compile("[\\d]*[\\.]{0,1}[\\d]*");
    Matcher m = p.matcher(ss);
    return m.matches();

    } catch (Exception e) {
    System.out.println("判断字符出错!" + e.getMessage());
    return false;
    }
    }
      

  3.   

    三楼的这样写,和我以前的一样会出现java.lang.NoClassDefFoundError:java/lang/CharSequence
    这样的错误提示,不知道是什么原因?有什么解决方法?
      

  4.   

    不是科学计数法的话
    Pattern p = Pattern.compile("^\\d*(\\.\\d+)?$");
      

  5.   

    我把你的代码贴到我的机器上试了一下,没有出错啊!我是有JCreator3.5环境下执行的,能够通过.
    import java.util.regex.*;
    public class Testclass2{
    public boolean getPan(String ss){
    boolean bool=false;
    //String regEx = "[\\.\\d]*";
    try{
    Pattern p=Pattern.compile("\\d++|\\d++\\.d++");
    Matcher m=p.matcher(ss);
    boolean bo=m.find();
    if (bo){ bool=true; }
    return bool;
    }catch(Exception e){
    System.out.println("判断字符出错!"+e.getMessage());
    return bool;
    }
    } public static void main(String[]args){
    Testclass2 t1=new Testclass2();
        if (t1.getPan("213.2")) System.out.println("ok");
        if (t1.getPan("xyyz")) System.out.println("ok"); else System.out.println("no");
        if (t1.getPan("yyzz123.234.12")) System.out.println("ok");else System.out.println("no");
        
        
    }
    }
      

  6.   

    CharSequence和Pattern都是since jdk1.4,
    如果Pattern都没问题不可能CharSequence找不到
      

  7.   

    [showone] 的模式写的比较准确,我已经试了一下.Pattern p = Pattern.compile("[\\d]*[\\.]{0,1}[\\d]*");
      

  8.   

    Pattern p = Pattern.compile("[\\d]*[\\.]{0,1}[\\d]*");
    ----------
    这个模式会对?
    a12.34b这个是小数?
    能够匹配的吧
      

  9.   

    java.lang.NoClassDefFoundError:java/lang/CharSequencejava/lang/CharSequence在jdk1.4后才有的,可能你的jdk版本太低了.