我得到了一个包含数字0的字符串。知道吗,我需要将它解析为一个int,所以我尝试了以下方法:
int oldfollowcounter = Integer.parseInt(followerzahl);followerzahl是字符串。
我总是会犯这个错误:
Exception in thread "Timer-4" java.lang.NumberFormatException: For input string: "0
"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at YBot.MyBot$3.run(MyBot.java:472)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)而本地的int只是空的。
有什么想法吗?
字符串包含:
followerzahl=0
followerzahl=0
followerzahl=0
followerzahl=0发布于 2014-02-25 14:07:25
Integer.parseInt是一个非常严格的解析器,它不会解析String,除非它只包含一个有效的整数,而不包含其他任何内容。
您需要先删除字符串中的其他内容。在本例中,数字后面似乎有空白(行提要),因此followerzahl.trim()就足以删除空白。如果有更多的字符(如引号、标记或其他字符),您将需要编写一些东西来提取包含数字的字符串的位,然后解析它。
发布于 2014-02-25 14:13:43
问题是“0"或"0."。
那么让我们看看它,Integer.parseInt只允许数字,所以小数点,附加的空格是非法的。
因此,NumberFormatException
您应该确保您的输入确实是
整数(即"20")
或者如果你真的想允许小数,那就用
Double.parseDouble或Float.parseFloat
或者,如果发现任何空格“0”,您应该首先使用
Integer.parseInt(followerzahl.trim()),
或者Double.parseInt(followerzahl.trim()),这应该是完美的。
发布于 2014-02-25 14:05:32
int oldfollowcounter = Integer.parseInt(followerzahl);followerzahl字符串必须是整数。
使用以下代码:
String followerzahl = "0";
int oldfollowcounter = Integer.parseInt(followerzahl);
System.out.println(oldfollowcounter);https://stackoverflow.com/questions/22016489
复制相似问题