【发布时间】:2012-01-16 13:19:20
【问题描述】:
我正在尝试从 String 数组元素中解析 int。这是我的代码:
String length = messageContents[k].replace("Content-Length:", "").replace(" ", "");
System.out.println("Length is: " + length);
int test= Integer.parseInt(length);
System.out.println 返回以下内容:长度为:23
但是,当我尝试将 String 解析为 int 时,会抛出 java.lang.NumberFormatException;
java.lang.NumberFormatException: For input string: "23"
我有点困惑如何将 23 解析为 int。我只能假设那里有其他角色在阻止它,但我一辈子都看不到它。
有什么建议吗?
谢谢
更新
Despite the String length having only two characters, Java reports its length as three:
Length is: '23'
Length of length variable is: 3
length.getBytes = [B@126804e
【问题讨论】:
-
我注意到你有一个尾随的“。”在第一行。也许是复制过去的错误?
-
啊,是的,忽略它。确实是复制粘贴错误。谢谢:)
-
“我只能假设那里有其他角色在阻止它,但我一辈子都看不到它。” 1) 附上
println语句中的值的单引号。 2)System.out.println("How long is this piece of String? " + length.length()); -
感谢安德鲁。长度是:23 长度是:3 那么额外的字符到底在哪里......它没有出现在输出中?
-
length.getBytes() 得到了什么?我希望 (byte[]) [50, 51]