62,620
社区成员
发帖
与我相关
我的任务
分享public class TestException1 {
public static void main(String args[]){
String str="222";
TestException1.stringToInt(str, 0);
}
public static int stringToInt(String str, int defaultValue){
int result = 0;
try{
result = Integer.parseInt(str);
}catch(NumberFormatException e){
//e.printStackTrace();
System.out.println("参数不能转化为Integer型,以默认值输出");
result = defaultValue;
}
return result;
}
}
public static int stringToInt(String str, int defaultValue){
try {
return Integer.parseInt(str);
} catch (Exception ex) {
return defaultValue;
}
}