capri9
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
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
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?
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?
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-di git-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,807 L)
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.
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.
ASKER
This is the strange error am seeing.
Caused by: java.lang.NumberFormatExce ption: For input string: "10000000000000000000"
at java.lang.NumberFormatExce ption.forI nputString (NumberFor matExcepti on.java:48 )
at java.lang.Long.parseLong(L ong.java:4 15)
at java.lang.Long.valueOf(Lon g.java:518 )
Caused by: java.lang.NumberFormatExce
at java.lang.NumberFormatExce
at java.lang.Long.parseLong(L
at java.lang.Long.valueOf(Lon
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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