Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Strange behavior of long datatype in Java

Posted on 2011-10-18
10
Medium Priority
?
382 Views
Last Modified: 2012-05-12
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
0
Comment
Question by:capri9
10 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 36985279
Please post full stack trace
0
 
LVL 47

Expert Comment

by:for_yan
ID: 36987180
>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
 
LVL 4

Expert Comment

by:stachenov
ID: 36987230
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

Expert Comment

by:for_yan
ID: 36989834
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
 

Author Comment

by:capri9
ID: 36992731
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 36992772
That's greater than Long.MAX_VALUE, so wont parse
0
 
LVL 10

Expert Comment

by:gordon_vt02
ID: 36993052
You may want to use BigInteger instead of Long.  It can hold any length number.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 36993123
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
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 36993155
>>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
 
LVL 47

Expert Comment

by:for_yan
ID: 36994034
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

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month15 days, 13 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question