数字格式转换字符串时的异常

时间:2013-03-10 01:12:14

标签: java runtime-error numberformatexception string-conversion

下面的代码将不会运行并继续抛出数字格式异常,错误是“线程中的异常”主“java.lang.NumberFormatException:对于输入字符串:”SAN 1905 1808 + 24 1512 + 17 1209 +10 1708-06 2016-16 211831 211941 192652“”“

intAlt = Integer.parseInt(strAlt);

我不确定为什么会发生这种情况,并希望对此事有任何建议

private int getPos(String strAlt)
{
 int intAlt;
 int intPos =0;
 intAlt = Integer.parseInt(strAlt);
 switch (intAlt)
 {
  case 3:
   intPos = 4;
   break;
  case 6:
   intPos = 9;
   break;
  case 9:
   intPos = 17;
   break;   
  case 12:
   intPos = 25;
   break;
  case 18:
   intPos = 33;
   break;   
  case 24:
   intPos = 41;
   break; 
  case 30:
   intPos = 49;
   break;   
  case 34:
   intPos = 56;
   break;   
  case 39:
   intPos = 63;
   break;  
 }
 return intPos;
}

1 个答案:

答案 0 :(得分:0)

如果您将“SAN 1905 1808 + 24 1512 + 17 1209 + 10 1708-06 2016-16 211831 211941 192652”传递给getPos(String strAlt),它将执行:

intAlt = Integer.parseInt(strAlt);

当然会抛出异常。 See this doc