我得到一个包含数字 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
最佳答案
Integer.parseInt
是一个非常严格的解析器,它不会解析 String
除非它只包含一个有效的整数而没有其他内容。
您需要首先删除字符串中可能存在的任何其他内容。在这种情况下,数字后面似乎有空格(换行符),因此 followerzahl.trim() 足以删除空格。如果有更多字符(例如引号或其他字符),您将需要编写一些内容来提取字符串中包含数字的位,然后对其进行解析。
关于java - 将包含数字的字符串解析为 INT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22016489/