Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4044002
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:11:55+00:00 2026-05-20T13:11:55+00:00

Hopefully a very simple query, but it’s left me scratching my head. I have

  • 0

Hopefully a very simple query, but it’s left me scratching my head.

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.

// 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);
  ...

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.

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T13:11:56+00:00Added an answer on May 20, 2026 at 1:11 pm

    Note that DatagramPackate.getData() returns the whole buffer!

    The data you received is only a part of it:

    The data received or the data to be sent starts from the offset in the buffer, and runs for length long.

    So to convert the data to a String you should use this constructor:

    String message = new String(pac.getData(), pac.getOffset(), pac.getLength(), "UTF-8");
    

    Note that I specify the UTF-8 encoding here, as not specifying an encoding would result in the platform default encoding to be used, which is generally not what you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully very simple SQL question - I'm just blacking out :) I have a
I'm developing a very simple but (hopefully) solid framework that just fits my needs.
This is hopefully a very simple question, but one a lot of Googling has
I have a terrible problem that hopefully has a very simple answer. I am
I'm currently writing a very simple JavaFX application which will hopefully display information from
This isn't a very simple question, but hopefully someone has run across it. I
This is hopefully a very simple maths question. If I have two number ranges,
Hopefully a very simple question. But I'm using Code First with an MVC app
I have just started looking at javascript so hopefully this will be something simple.
Hopefully a question with a very simple answer, but it's not one that I've

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.