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

字符转换数字问题。
String s="yg";
System.out.println(Integer.parseInt(s));

会报java.lang.NumberFormatException: For input string: "yg"异常。
怎么对S进行预判断,保证S里面存的是数字类型的字符,可以paerInt();
不想用try做异常检测!

------解决方案--------------------
用Asc码一个一个字符判断.或用正则表达式判断.
------解决方案--------------------
public boolean isNum(String str)
{
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() )
{
return false;
}
return true;
}