现在的位置: 首页 > 综合 > 正文

java验证数字

2013年10月08日 ⁄ 综合 ⁄ 共 565字 ⁄ 字号 评论关闭

1.通过强制转换: 
1、    private boolean vidateIsNumber(String str) { 
try { 
Long.parseLong(str); 

return true; 

} catch (Exception e) { 
// TODO: handle exception 
return false; 


2.用正则表达式 
首先要import java.util.regex.Pattern 和 java.util.regex.Matcher 
这两个包,或者不导入包,直接用: 
//正则表达式数字验证 
    public boolean isNumber(String str) 
    { 
        java.util.regex.Pattern pattern=java.util.regex.Pattern.compile("[1-9][0-9]*"); 
        java.util.regex.Matcher match=pattern.matcher(str); 
        if(match.matches()==false) 
        { 
           return false; 
        } 
        else 
        { 
           return true; 
        } 
    }

抱歉!评论已关闭.