NumberFormatException with Integer.parseInt() [duplicate]

﹥>﹥吖頭↗ 提交于 2019-12-13 07:46:59

问题


I've a problem with Integer.parseInt(). Specifically my code do this:

serverPort variable is an int correctly initialized to 1910

byte[] multicastMessage = (serverAddress+"::"+String.valueOf(serverPort)).getBytes();

byte[] receivedBytes = receivePacket.getData();
receivedString = new String(receivedBytes, "UTF-8");

String[] decodedString = receivedString.split("::");            
serverPort = Integer.parseInt(decodedString[1]);

Note that when I print decodedString[1] in console is correctly printed 1910. But when I call Integer.parseInt() a NumberFormatException is raised.

I've tried also using Integer.toString(serverPort) in first row or using new Integer(decodedString[1]).intValue() in last row without success.

I suspect the conversion issue born using byte (I can't avoid it), but I'm not so familiar with byte struct.

EDIT:

Exception in thread "Thread-0" java.lang.NumberFormatException: For input string: "1910"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at ClientThread.run(ClientThread.java:60)

回答1:


I see your comment that trim() is still providing the NumberFormatException.

My next guess is that there is an invisible ASCII character such as a BOM (bye order mark) somewhere in your String. The best way to check this would be to run your string through the following function:

public static String displayCharValues(String s) {
StringBuilder sb = new StringBuilder();
for (char c : s.toCharArray()) {
    sb.append((int) c).append(",");
}
return sb.toString();}

If a BOM is present then you will see 65279 printed out as part of the sequence. If your String contains valid numbers then you should only see the corresponding ASCII codes assocatied with numbers (http://www.asciitable.com/). You should see your 1910 string print out as 49,57,49,48.




回答2:


As @azurefrog pointed out, this is likely a whitespace issue. The following program parses correctly:

    String receivedString = "host::1910";

    String[] decodedString = receivedString.split("::");

    int serverPort = Integer.parseInt(decodedString[1]);

    System.out.println(serverPort);

However, if you add whitespace before 1910 then it throws a NumberFormatException like you indicated. The solution would be to use the String.trim to remove any white space.

    String receivedString = "host:: 1910";

    String[] decodedString = receivedString.split("::");

    int serverPort = Integer.parseInt(decodedString[1].trim());

    System.out.println(serverPort);



回答3:


using solution posted by Justin L I've noted that my string appears as:

49,57,49,48,0,0,0,0,0,0,0,0,0,0,......many others 0.

(0 = blank spaces?)

but anyway .trim() does not effect.

I resolved using a different format of string sent by my server. Instead of: string+"::"+stringVersionOfInt I've used: string+"::"+stringVersionOfInt+"::"

now my string is correctly parsed from Integer.parseInt()

That's works! Thank's to all!



来源:https://stackoverflow.com/questions/40311348/numberformatexception-with-integer-parseint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!