首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

判断String是不是数字,该怎么处理

2012-01-07 
判断String是不是数字怎么判断String是不是数字,如果不是数字强制转换成int类型好像会出错。[解决办法]publ

判断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

热点排行
Bad Request.