File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes problems with BigInteger Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problems with BigInteger" Watch "problems with BigInteger" New topic

problems with BigInteger

Richard Gibson

Joined: Mar 16, 2000
Posts: 9
in assignment 1.6:
I can create the list, and the final number for square 64 is
So I am assuming I need to use the BigInteger class from the java.math.* package to get rid of that "-".
I have tried various combinations but to no avail. For example I tried using the method valueOf(long val) as follows:
public static BigInteger valueOf(long x)
return x;
in my "Grains" class and got the reply:
"Incompatible type for return. can't convert long to java.math.BigInteger."
which is what I thought this method was for!
I also looked at the pow(int exponent) method which looks like it could do all the work except that I too am lost with the "this" part. Can someone point me on the right track? Have I even chosen the right package?!
Ray Marsh
Ranch Hand

Joined: Jan 12, 2000
Posts: 458
If you want to use the BigInteger class you need to create an instance of it.
Example: BigInteger bi = new BigInteger
Then use the methods of BigInteger on that object.

Anxiety does not empty tomorrow of its sorrows, but only empties today of its strength. – Charles Spurgeon
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
In this case valueOf is a static method if BigInteger, so no instance is necessary.
Richard- if I understand what you're asking and doing, then you're creating your own definition of valueOf. This is unnecessary as the method is already defined in BigInteger to do what you want. You can use it like this:
<pre> BigInteger bi = BigInteger.valueOf(someValue);</pre>
where someValue can be any integral type (it will be implicitly converted to long if necessary).

"I'm not back." - Bill Harding, Twister
Richard Gibson

Joined: Mar 16, 2000
Posts: 9
I have managed to get BigInteger to work in the following code:

which works (i.e. it compiles), but the result is still the -9.... number above. Is the problem that I need to do my multiplications directly with BigInteger. I tried the bi = (bi * 2) and, of course, got the message that BigInteger is NOT an int.

[This message has been edited by Paul Wheaton (edited March 21, 2000).]
paul wheaton

Joined: Dec 14, 1998
Posts: 20972

I deleted some code that might give future cattle drive people too much info...
(I would answer questions, but my boss wants me and I have to go.)

permaculture Wood Burning Stoves 2.0 - 4-DVD set
paul wheaton

Joined: Dec 14, 1998
Posts: 20972

Spend some more time at
I agree. Here's the link:
subject: problems with BigInteger
It's not a secret anymore!