Java parseInt NumberFormatException

时间:2014-05-05 19:03:08

标签: java numberformatexception parseint

我试图通过parseInt将数组中的字符串值分配给整数,并且我收到此错误。有什么建议吗?

System.out.println("in the loop for loading info");

try
{
    String testnumber = "1  ";
    System.out.println("Numeric value of testnumber is:" + Integer.parseInt(testnumber.trim()));
    System.out.println("String value from array is:" + Global.teamPlayerHandicap[row][0].trim());

    int testvalue = Integer.parseInt(Global.teamPlayerHandicap[row][0].trim());
    System.out.println("Test value:" + testvalue);
}
catch (NumberFormatException e)
{
    System.out.println("This is not a number:" + Global.teamPlayerHandicap[row][0].trim() +"END");
    System.out.println(e.getMessage());
}

输出:

in the loop for loading info

Numeric value of testnumber is:1

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: For input string: "1"

String value from array is:1

This is not a number:1END

For input string: "1"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
    at java.lang.Integer.parseInt(Integer.java:449)
    at java.lang.Integer.parseInt(Integer.java:499)
    at com.snafilter.TGL.DoWorkTGL1.PopulateSubmitScoresInfo(DoWorkTGL1.java:1271)

1 个答案:

答案 0 :(得分:0)

尝试使用指定的字符编码

String trimStr = Global.teamPlayerHandicap[row][0].trim();
byte[] bytes = trimStr.getBytes();
String str = new String(bytes, Charset.forName("UTF-8"));

int testvalue = Integer.parseInt(str);
System.out.println(testvalue);

有关详细信息,请查看NumberFormatException error (parseInt)