Java parseInt integrity check
•
Java
I'm trying to parse an int from a string array element. Here's my code:
String length = messageContents[k].replace("Content-Length:","").replace(" ",""); System.out.println("Length is: " + length); int test= Integer.parseInt(length);
System. out. Println returns the following: length: 23
However, when I try to parse a string into int, I throw a Java lang.NumberFormatException;
java.lang.NumberFormatException: For input string: "23"
I'm a little confused about how 23 translates into int. I can only assume that some other characters are blocking it, but I can't see it in my life
Any suggestions?
thank you
to update
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
Solution
Try this variant:
int test= Integer.parseInt(length.trim());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码