public class StringException {
public static void main(String args[]){
int res ;
String str = "";
try{
res = string2Int(str = new String("123456"));
System.out.println(res);
res = string2Int(str =new String("1234 56"));
System.out.println(res);
res = string2Int(str =new String("1234fg"));
System.out.println(res);
}catch(NumberFormatException e){
System.out.println("数据"+str+"无法被转换成整数");
}
}
public static int string2Int(String str){
int i = new Integer(str.replace(" ", ""));
return i;
}
}
这样?
这个不能转整数,你要的是判断条件吗?只能整数字符串才转换的?