// 转换错误,下面会出现 java.lang.ClassCastException错误,改用int iInc = Integer.parseInt(m.get("INCREMENTVAL").toString());
// 1 int iInca = Integer.valueOf((String) m.get("INCREMENTVAL")).intValue();
// 2 int iInc = Integer.parseInt(m.get("INCREMENTVAL").toString());第一句,在main方法测试没有问题不会出现异常讲讲两句的区别,还有为什么第一局回出现异常
// 1 int iInca = Integer.valueOf((String) m.get("INCREMENTVAL")).intValue();
// 2 int iInc = Integer.parseInt(m.get("INCREMENTVAL").toString());第一句,在main方法测试没有问题不会出现异常讲讲两句的区别,还有为什么第一局回出现异常
楼主【leomei001】截止到2008-07-28 09:58:37的历史汇总数据(不包括此帖):
发帖的总数量:0 发帖的总分数:0 每贴平均分数:0
回帖的总数量:0 得分贴总数量:0 回帖的得分率:0%
结贴的总数量:0 结贴的总分数:0
无满意结贴数:0 无满意结贴分:0
未结的帖子数:0 未结的总分数:0
结贴的百分比:---------------------结分的百分比:---------------------
无满意结贴率:---------------------无满意结分率:---------------------
如何结贴请参考这里:http://topic.csdn.net/u/20080501/09/ef7ba1b3-6466-49f6-9d92-36fe6d471dd1.html
public static Integer valueOf(String s) throws NumberFormatException
{
return new Integer(parseInt(s, 10));
}这个是valueOf方法,第一句话中,它在parseInt的时候传了第2个参数,会将这个字符串转换成10进制的数字
并且new Integer返回回去,而第一句话中的intValue其实还没有调用就已经出现异常了.在第二句话中,
public static int parseInt(String s) throws NumberFormatException {
return parseInt(s,10);
}
是直接返回了parseInt的结果,返回的是一个int.
在看原代码的时候,发现错误出现在下边的parseInt函数中public static int parseInt(String s, int radix)
throws NumberFormatException
{
if (s == null) {
throw new NumberFormatException("null");
} if (radix < Character.MIN_RADIX) {
throw new NumberFormatException("radix " + radix +
" less than Character.MIN_RADIX");
} if (radix > Character.MAX_RADIX) {
throw new NumberFormatException("radix " + radix +
" greater than Character.MAX_RADIX");
} int result = 0;
boolean negative = false;
int i = 0, max = s.length();
int limit;
int multmin;
int digit; if (max > 0) {
if (s.charAt(0) == '-') {
negative = true;
limit = Integer.MIN_VALUE;
i++;
} else {
limit = -Integer.MAX_VALUE;
}
multmin = limit / radix;
if (i < max) {
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
} else {
result = -digit;
}
}
while (i < max) {
// Accumulating negatively avoids surprises near MAX_VALUE
digit = Character.digit(s.charAt(i++),radix);
if (digit < 0) {
throw NumberFormatException.forInputString(s);
}
if (result < multmin) {
throw NumberFormatException.forInputString(s);
}
result *= radix;
if (result < limit + digit) {
throw NumberFormatException.forInputString(s);
}
result -= digit;
}
} else {
throw NumberFormatException.forInputString(s);
}
if (negative) {
if (i > 1) {
return result;
} else { /* Only got "-" */
throw NumberFormatException.forInputString(s);
}
} else {
return -result;
}
}
红色的是出错部分...自己看看吧..
if (digit < 0) {
throw NumberFormatException.forInputString(s);
} else {
result = -digit;
}
怎么没标出来,这是出错的部分
int iInca = Integer.valueOf((String) m.get("INCREMENTVAL")).intValue(); 这个抛ClassCastException异常,是因为valueOf()方法要返回一个Interger,也就是把一个string转换成一个Integer,如果转换出错,就会抛这个异常。
原代码里写出来的