• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Numbers

 
Edmond Corrola
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm reading in positive numbers from a file, I need to do some processing but there's no telling how big these positive numbers will be. So far it looks like up to a max of 10 digits. I've tried declaring different variables to hold them (int, float, long, BigInteger, etc..) but I get a compile time error of "number too large" when I test an integer value of 10 digits on them. What can I use to hold huge numbers?

p.s. I was thinking of just storing them as a string, than processing the parts I need by breaking the string up (because the number represents an address which I need to break into bits). Does this sound like a better solution?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's a compile-time error, that suggests you're writing something like

long x = 123456789012345;

and getting the error. That's because to write a long literal in Java code, you have to mark the actual literal as being a long by appending a letter 'L':

long x = 123456789012345L;

Try it!
 
Edmond Corrola
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, in C i always type casted it so i wouldn't have to remember. I completely forgot that I needed to do that. It wasn't making sense that I couldn't use a 10 digit number for a "long".
[ October 24, 2008: Message edited by: Edmond Corrola ]
 
Rob Spoor
Sheriff
Posts: 20837
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And if long becomes too small, you can always use java.math.BigInteger. Not as fast but at least it can hold arbitrarily large numbers.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!