首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java parseInt NumberFormatException

Java parseInt NumberFormatException
EN

Stack Overflow用户
提问于 2014-05-06 03:03:08
回答 1查看 3.1K关注 0票数 0

我试图通过parseInt将数组中的字符串值赋给整数,但我得到了这个错误。有什么建议吗?

代码语言:javascript
复制
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());
}

输出:

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2014-05-06 03:17:20

尝试使用指定的字符编码

代码语言:javascript
复制
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)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23479829

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档