This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Java Numbers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Numbers" Watch "Java Numbers" New topic

Java Numbers

Edmond Corrola

Joined: May 23, 2008
Posts: 15
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

Joined: Jul 08, 2003
Posts: 24195

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!

[Jess in Action][AskingGoodQuestions]
Edmond Corrola

Joined: May 23, 2008
Posts: 15
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

Joined: Oct 27, 2005
Posts: 20183

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.

How To Ask Questions How To Answer Questions
subject: Java Numbers
It's not a secret anymore!