NumberFormatException with Integer.parseInt() [dup

2019-09-20 05:30发布

问题:

This question already has an answer here:

  • What is a NumberFormatException and how can I fix it? [duplicate] 9 answers

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!