-1

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)
Akinn
  • 1,896
  • 4
  • 23
  • 36
  • My hunch is you need to trim your input: `serverPort = Integer.parseInt(decodedString[1].trim());`. It's hard to say without the stacktrace, which you have not provided. – azurefrog Oct 28 '16 at 18:45
  • 1
    If `decodedString[1]` is equal to `"1910"` then `Integer.parseInt` **cannot** throw an exception. So, if an exception is thrown, what does that imply? – Tunaki Oct 28 '16 at 18:45
  • Added stack trace. With trim() I've same result. "1910" is NOT equal to decodedString[1] – Akinn Oct 28 '16 at 19:09
  • print decodedString[1].length() – xenteros Nov 02 '16 at 12:24

3 Answers3

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.

Justin L
  • 375
  • 2
  • 10
  • You are perpetuating confusion by referring to ASCII when Java strings are counted sequences of UTF-16 code units. Also, a BOM is only a BOM at the beginning of a stream or file. An appropriate reader would not carry it into a string value because it is not data. (That's not to say that it can be ruled out as a problem, though.) Obviously, parsetInt is not broken so, as you suggest, the key is to find out which UTF-16 code units are being passed to it. – Tom Blodget Oct 30 '16 at 01:33
0

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);
Justin L
  • 375
  • 2
  • 10
0

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!

Akinn
  • 1,896
  • 4
  • 23
  • 36