Link to home
Start Free TrialLog in
Avatar of capri9
capri9Flag for India

asked on

Strange behavior of long datatype in Java

Dear Friends,

I declared a long variable in java application and passing 19 digits number as parameter, it is working fine in Jnuit. but when I test the same java application in Unix environment using xls input file passing the same 19 digit number, it is throwing numberFormat exception. Strange behavior.
Can any one have idea about this strange behavior?

Thanks in Advance
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Please post full stack trace
>the same java application in Unix environment using xls input file

what kind of .xls file are you reading in Unix environement and what are you using to read this file - is it POI or something else?
Avatar of stachenov
stachenov

Try to create a small sample code to test it. Something like a class with only one line in main() containing just Long.parseLong("your-19-digit-number-goes-here"). This way you'll be able to confirm if this the long datatype issue or something else entirely. It seems unlikely to me that parsing code could be platform-dependent.
19-digit is just where the maximum possible long number is (9,223,372,036,854,775,807L)
so one thing is to make sure that both 19-digit numbers which you used in different environments are the same

Besides, while java should be system neutral in the sense of maximum number, if you have some interaction with native code
(when doing something wiith .xls files),  then there could be some issues.
So, pelase, post more details.
Avatar of capri9

ASKER

This is the strange error am seeing.
Caused by: java.lang.NumberFormatException: For input string: "10000000000000000000"
      at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
      at java.lang.Long.parseLong(Long.java:415)
      at java.lang.Long.valueOf(Long.java:518)
That's greater than Long.MAX_VALUE, so wont parse
You may want to use BigInteger instead of Long.  It can hold any length number.
capri9, fyi, number parsing/formatting in Java is essentially broken: Java shouldn't care as long as the long can be parsed into the number of bits allowed for that value - but it DOES - and throws an error if it's > Long.MAX_VALUE
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your number is not 19 digits - it is 20 digits -
10000000000000000000

 it is buigger than max Long which is 9,223,372,036,854,775,807L
Remove one zero and it will work.

This one could not have worked in other OS either - as it is bigger than waht Java allows
Eeither use bigInteger or some other way