my dog learned polymorphism*
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problems with BigInteger" Watch "problems with BigInteger" New topic
Author

problems with BigInteger

Richard Gibson
Greenhorn

Joined: Mar 16, 2000
Posts: 9
in assignment 1.6:
I can create the list, and the final number for square 64 is
-9223372036854775808
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?!
thanks,
Richard
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
Wanderer
Sheriff

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
Greenhorn

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

STUFF DELETED
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.
R

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

Joined: Dec 14, 1998
Posts: 20494
    ∞

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
Trailboss

Joined: Dec 14, 1998
Posts: 20494
    ∞

Spend some more time at http://java.sun.com/products/jdk/1.2/docs/api/java/math/BigInteger.html
 
wood burning stoves
 
subject: problems with BigInteger
 
Similar Threads
extractFromRequest(req)
int to BigInteger
overloading confusion
valueOf
Initial value for BigInteger from another BigInteger