I observed that many people out there visiting my blog have great interest in a particular NumberFormatException. It goes by the message Character array is missing „e“ notation exponential mark and was mentioned in my post about fuzzing in Java.
Today we will take a more in depth look at it.
How does that NumberFormatException look like?
To visualize the error effectively, let’s look at the following extreme example drawn from my fuzzing post mentioned above.
Consider the following (sub-par implemented) function dollar2euro
that takes any input (hopefully it’s a number though!) and tries to convert it from USD to EUR:
public String dollar2euro(Object input){
BigDecimal inputParsed = new BigDecimal(input.toString());
BigDecimal dollars = inputParsed.setScale(2, BigDecimal.ROUND_HALF_EVEN);
BigDecimal multiply = dollars.multiply(BigDecimal.valueOf(0.92));
BigDecimal euros = multiply.setScale(2, BigDecimal.ROUND_HALF_EVEN);
return String.valueOf(euros);
}
Now what happens, if we play along the method signature and put in the following characters: „뤇皽“? If you know what they mean, feel free to drop me a comment down below. Please note that this is exactly what my fuzztest tried to do. And of course it drops a heavy NumberFormatException upon us:
Input: 뤇皽 java.lang.NumberFormatException: Character 뤇 is neither a decimal digit number, decimal point, nor "e" notation exponential mark.
And what does it mean?
The problem here is that „뤇皽“ is not a number. The first and second part Character 뤇 is neither a decimal digit number, decimal point are straight forward. But what does the third part nor „e“ notation exponential mark mean?
In maths, we have the option of expressing numbers in power form with base 10. This comes in handy when we want to express Googol (the number that inspired Google’s name) – a 1 with 100 zeroes. Accordingly we would write 10100 instead. In Java you can do something very similar, but there are better sources to check how to properly use the e-format. For our context let’s acknowledge that it is a different way of displaying numbers. Which likewise could not be found in our input.
But how do we fix that?
We have to make sure that we provide a String
to the BigDecimal
Constructor that looks like a valid number – if we want to provide a String
at all. An actual number like a double
or int
would be even better. That would cost us no more than a change in the method’s signature and a small adaption to line 2. If we really want to provide a String
, we still can do that, but then we have to make sure that it is properly formatted. A valid input example would be: „1337.012342
„.
If you apply this simple rule, you should be spared from ‚Character array is missing „e“ notation exponential mark‘ errors in the future.
Conclusion
So long! I hope this little post gave you an idea about what the error message ‚Character array is missing „e“ notation exponential mark‘ means. Of course this case was kinda constructed and you probably have a less explicit case. If so, feel free to post it in the comments down below and we see what we can do. But for demonstration purposes it should have provided a clear image of what is going on in your program.
As my linked post about fuzzing in Java implies, I’m an avid test automation person. If you want more about automating tests with Java, check out my introduction tutorial about Selenium in Java. If you are more of the Python person, no problem. Here’s the same Selenium tutorial in Python. And if Python and Java are both too mainstream for you, I recommend my tutorial about Cucumber in Rust .
Happy test automating & have a nice day!