4

In my program I need to convert a String to Int.

    String str = new String(request.getData());

    String [] setting = str.split(" ");        
    String bs = setting[1];

The value of bs is 1024, I use System.out.println to test it, and it displays on the screen with "1024".

But when I use

    int blockSize = Integer.parseInt(bs); 

it will return an exception point to the line of Integer.parseInt :

Exception in thread "main" java.lang.NumberFormatException: For input string: "1024"
  at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
  at java.lang.Integer.parseInt(Integer.java:458)
  at java.lang.Integer.valueOf(Integer.java:554)

Can someone help me to solve it? Thanks.

6
  • Are you sure bs contains 1024, because if so, then you shouldn't be getting any problem. Commented Sep 20, 2014 at 7:15
  • 1
    @darknight, I don't think it's an issue of excessive whitespace since he's using a space as a delimiter. Commented Sep 20, 2014 at 7:17
  • i think your bs contains string value not a integer that's why this error. make sure setting[postion] is an integer value. i mean like "somevalue". Commented Sep 20, 2014 at 7:22
  • 7
    @jacobbb - I suspect you have invisible characters in there. bs.length() would probably be helpful, and possibly even a loop over the characters outputting their character code: for (char ch : bs.toCharArray() ) { System.out.println(Integer.toHexString(ch)); } Commented Sep 20, 2014 at 7:23
  • 2
    I think the issue is with some unicode hidden character. Test the length of the string and check if it is really 4. Commented Sep 20, 2014 at 7:23

3 Answers 3

10

I suspect you have some hidden unicode character in the string bs, you can remove the non-digits with:

bs = bs.replaceAll("\\D", "");
int blockSize = Integer.parseInt(bs);

The code above will also convert the string "1a2" to 12, but that doesn't seem your case.

Sign up to request clarification or add additional context in comments.

2 Comments

I tried bs.length(), the return value is 1019... and I used "bs = bs.replaceAll("\\D", "");" , problem solved, thanks.
yeah..1019..idk why, the array setting[] is i sending from datagramSocket, is there sth to do with this?
1

try this code:

 String bs = setting[1].trim().toString();

Comments

0
    while( (!bs.matches("\\d+")) && (bs.length > 1)) 
    {
        bs = bs.substring(1);
    }

    if (bs.matches("\\d+")
    {
        int blockSize = Integer.parseInt(bs);
    }
    else
    {
        int blockSize = -1;
    }
    /* !! Then, check for "-1" in your application of 
          block size #runtime_exception_prevention */

This will continue to remove the offensive non digit bits down to 1, as necessary, until a digit is found or only one character remains in the string. The second check prevents the exception and returns a flagged value. Checking for this flagged value will intercept runtime exceptions. NB: I wrote this code in the comment block, please forgive any minor errors, I will gladly correct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.