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
capri9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Please post full stack trace
0
for_yanCommented:
>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?
0
stachenovCommented:
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.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

for_yanCommented:
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.
0
capri9Author Commented:
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)
0
CEHJCommented:
That's greater than Long.MAX_VALUE, so wont parse
0
gordon_vt02Commented:
You may want to use BigInteger instead of Long.  It can hold any length number.
0
CEHJCommented:
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
0
CEHJCommented:
>>as long as the long can be parsed into the number of bits allowed for that value

I'm thinking *particularly* where hex values/inputs are concerned

As Gordon said (if you've got the control), BigInteger can help:
long negLong = new BigInteger("10000000000000000000").longValue(); // perfectly OK

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
for_yanCommented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.