Search code examples
javaeclipseexceptionnumberformatexceptionjava.lang.class

java.lang.NumberFormatException: For input string: “ ”


So the issue I am getting seems to be a kind of common, however the missing points in each of the issues I checked online was different from the one I experiencing.

So what I am trying to do is converting a string back to long. The string was read from a file as following

484625517161611266 string 454511457536

I read the line into an array and I printed the array which shows the writing went fine. Then I tried convert to long as below

long id = Long.parseLong(splitted[0],10);

However I got this issue

java.lang.NumberFormatException: For input string: "484625517161611266"

Normally the common issues with this type of exception would be trying to convert letter or trying to convert numbers that contains spaces. Another issue might be a certain mistake while reading the file.

However I check all of those possibilities and still I am getting this exception

Any Suggestions ? Thanks in advance


Solution

  • java.lang.NumberFormatException: For input string: "484625517161611266"

    There is a \uFEFF (ZERO WIDTH NO-BREAK SPACE) character in your input

    Try copying it and pasting it in plain text editor, remove this junk character

    to remove this character you can use

    inputNumberString.replaceAll("\uFEFF", "").trim()
    

    note: trim() just to remove any other whitespace if present