I deal with a lot of file processing at my current day job that comes from many third party payment provides, aka banks. From time to time we run into issues where individual or batches of payment or sometimes fail for many reasons, including missing requirements, timing issues, connection issues, utf-16 character sets in the file shifting the locations of flat files, etc.

Most of these issues are very rare. Recently I was looking at a problem with one the payment providers and noticed the following exception

For input string: "08"
java.lang.NumberFormatException: For input string: "08"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.valueOf(Integer.java:740)
at java.lang.Integer.decode(Integer.java:1197)
at org.apache.commons.lang3.math.NumberUtils.createInteger(NumberUtils.java:664)

Quickly I jump to the conclusion that there was an issue with the file. The batching code associated this process has not been modified recently. Eyeing the data, everything seemed to be correct, there wasn't anything out of the ordinary.

After requesting the files from devops, I ran the following on my local to see if I can reproduce and pinpoint the issue.

I discovered the following line that was causing the issue. Keep in mind this code makes a lot of assumptions such as it never being null or does not contain spaces.

final var batchNumber = NumberUtils.createInteger(group*.BatchID.firstWhere(\elt -> elt != null))

Verifying via git, this code was added back in 2018, so why, all of a sudden, this particular batch number 08 decided to fail

Running a few test things seem to work until i got to batch 08

print(NumberUtils.createInteger("001"))
print(NumberUtils.createInteger("002"))
print(NumberUtils.createInteger("007"))
print(NumberUtils.createInteger("008"))

Output:

1
2
7
java.lang.NumberFormatException: For input string: "08"
 ...

Well, I wasn't expecting that. Lets take a look at the documentation of the NumberUtils.createInteger function

NumberUtils.createInteger

Convert a String to a Integer, handling hex (0xhhhh) and octal (0dddd) notations. N.B. a leading zero means octal; spaces are not trimmed.

There you go, we were using the wrong function. You can get away with the standard Integer.parseInt java had since day one. Keep in mind, this method doesn't handle strings with spaces or null values. It will throw an exception

You can learn about the Octal Number system here


You must log in to comment.