Note that there are some explanatory texts on larger screens.

plurals
  1. PONumberFormatException error (parseInt)
    text
    copied!<p>Hopefully a very simple query, but it's left me scratching my head.</p> <p>I have a string, which is just a single integer, and I'm trying to then get that integer out as an int. This on the face of it shouldn't be a problem.</p> <pre><code>// this is how I create the string (it's the playload from a UDP datagram packet, // thought I don't think the origins hugely important - it's juts a test run so the // stringMessage is always 1 (created by a seperate client process) ... recvSoc.receive(pac); String stringMessage = new String(pac.getData()); port = pac.getPort(); System.out.println("RECEIVED: " + stringMessage + " on port: " + port); processMessage(stringMessage); ... // Then in processMessage public void processMessage(String data) { int message; message = Integer.parseInt(data); ... </code></pre> <p>This always crashes with a NumberFormatException error. I cannot for the life of me figure out what's causing this, any ideas greatly appreciated. I haven't coded much in Java (recently) so might simply be forgetting something critical or what not.</p> <pre><code>Exception in thread "main" java.lang.NumberFormatException: For input string: "1" at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:481) at java.lang.Integer.parseInt(Integer.java:514) at udp.UDPServer.processMessage(UDPServer.java:85) at udp.UDPServer.run(UDPServer.java:52) at udp.UDPServer.main(UDPServer.java:156) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload