4

I keep seeing this in my logs, however to my knowledge the scenario should be impossible.

java.lang.NumberFormatException: For input string: "1487832810"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[na:1.7.0_80]
        at java.lang.Long.parseLong(Long.java:430) ~[na:1.7.0_80]
        at java.lang.Long.parseLong(Long.java:483) ~[na:1.7.0_80]

here is (i think) the relevant source code (the line numbers match to the ST)

424:    char firstChar = s.charAt(0);
425:    if (firstChar < '0') { // Possible leading "+" or "-"
426:        if (firstChar == '-') {
427:            negative = true;
428:            limit = Long.MIN_VALUE;
429:        } else if (firstChar != '+')
430:            throw NumberFormatException.forInputString(s);

Therefore, it thinks the first char (1) is < '0' ??

I can only assume it is some character encoding issue or something.

Any ideas?

pstanton
  • 35,033
  • 24
  • 126
  • 168
  • 3
    Perhaps there's a non-printable character before the `1`? Can you log the length of the number you call `Long.parseLong`? – Jon Skeet Feb 27 '17 at 07:02
  • 2
    It's not the if "1 < '0'" because there's another part of the method. It's probably a hidden character. – Maroun Feb 27 '17 at 07:03
  • 1
    Why are quotes coming before and after the string while displaying the error. Can it be a possibility that string might be "\"1487832810\"". Just asking. Display the length of the string and let us know. – Mirza Feb 27 '17 at 07:23
  • Or at least print out the first character of the string, – FredK Feb 27 '17 at 07:45
  • 2
    @gRaWEty good theory, but [`NumberFormatException.forInputString()`](http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/NumberFormatException.java#l65) adds those quotes. – dimo414 Feb 27 '17 at 07:50
  • thanks for the suggestions, definitely crap at the start of the string. – pstanton Feb 27 '17 at 11:09

1 Answers1

3

I think that is because there are non-printable characters in your string. All the non-printable characters are < '0'. You can see here which ones are printable and which ones are not:

http://web.itu.edu.tr/sgunduz/courses/mikroisl/ascii.html

That is the reason you can't see in the output the character that is raising the exception.

If you don't want to throw an exception for these cases you will have to do the comparison more accurate, like checking these characters in the if statement or with a regex. Anyway, there are several questions in SO to replace these characters, like these:

Fastest way to strip all non-printable characters from a Java String

How can I replace non-printable Unicode characters in Java?

Community
  • 1
  • 1
mlg
  • 5,248
  • 1
  • 14
  • 17
  • 1
    the output in the OP was from `tail` - i went in via `vi` to retreive the full ST and it showed a whole bunch of `^@` prior to the number! correct answer. now I have to find out where these chars are coming from!. thx. – pstanton Feb 27 '17 at 11:08