wood burning stoves*
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 OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Numbers" Watch "Java Numbers" New topic
Author

Java Numbers

Edmond Corrola
Greenhorn

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
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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
Greenhorn

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
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
wood burning stoves
 
subject: Java Numbers