日期:2014-05-20  浏览次数:20652 次

判断String是不是数字
怎么判断String是不是数字,如果不是数字强制转换成int类型好像会出错。

------解决方案--------------------
public boolean checkNum(String args){
Pattern p=Pattern.compile( "^[\d]* "); //正则表达式
Matcher m=p.matcher(args);
if(m.matches())
return true;
else
return false;
}

这是前几天一个帖子问过的,多翻翻旧帖。
------解决方案--------------------
public int checkNumber(String st){
int dipage=-1;
if(st.matches( "[\\d]* ")){
try{
dipage=Integer.parseInt(st);
}catch(Exception ex){
dipage=-1;
}
return st;
}else{
return dipage;
}
}
如果是数字就返回相应的数 如果不是返回-1