aspose file tools*
The moose likes Beginning Java and the fly likes BigInteger and multiplying Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BigInteger and multiplying" Watch "BigInteger and multiplying" New topic
Author

BigInteger and multiplying

V��r� Tunnus
Greenhorn

Joined: Mar 29, 2006
Posts: 8
Hi. I'm trying to use BigIntegers (I need them, normal Integers aren't enough) and multiply two BigIntegers but I get just errors. Also, I'm not really so sure about creating BigInteger values, so if I have something wrong, could you please tell me what I'm doing wrong. So here is my problematic code:
---------------------------------------------------------------------------

public static void main(String[] args) {
Scanner nappaimisto;
nappaimisto = new Scanner (System.in);

System.out.println("Anna luku 1:");
BigInteger luku1 = new BigInteger(nappaimisto.nextBigInteger());
System.out.println("Anna luku 2:");
BigInteger luku2 = new BigInteger(nappaimisto.nextBigInteger());
BigInteger tulo = new BigInteger.multiply(luku1, luku2);
System.out.println("Lukujen " + luku1 + " ja" + luku2 + " tulo on " + tulo);
}
---------------------------------------------------------------------------

I'm getting these errors:

---------------------------------------------------------------------------
Kertolasku.java:32: cannot find symbol
symbol : constructor BigInteger(java.math.BigInteger)
location: class java.math.BigInteger
BigInteger luku1 = new BigInteger(nappaimisto.nextBigInteger());
^
Kertolasku.java:34: cannot find symbol
symbol : constructor BigInteger(java.math.BigInteger)
location: class java.math.BigInteger
BigInteger luku2 = new BigInteger(nappaimisto.nextBigInteger());
^
Kertolasku.java:35: cannot find symbol
symbol : class multiply
location: class java.math.BigInteger
BigInteger tulo = new BigInteger.multiply(luku1, luku2);
^
3 errors
---------------------------------------------------------------------------

I've imported Scanner and BigInteger classes. I have also tried to read Java API etc. but this problem doesn't seem to solve just with my skills.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to JavaRanch!
cannot find symbol
symbol : constructor BigInteger(java.math.BigInteger)

...is telling you that there is no BigInteger constructor that takes a BigInteger as an argument. (In fact, if you already have a BigInteger...?)
cannot find symbol
symbol : class multiply
location: class java.math.BigInteger
BigInteger tulo = new BigInteger.multiply(luku1, luku2);

...is telling you that you cannot call the multiply method from the class BigInteger. This is because multiply is an instance method -- not a static method. So you need to call multiply on an instance of BigInteger. Also, multiply takes only one argument, which is the BigInteger that the instance is to be multiplied by. For example...

BigInteger tulo = luku1.multiply(luku2);


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
V��r� Tunnus
Greenhorn

Joined: Mar 29, 2006
Posts: 8
Ok. I think I understand the ending part of your answer, but then this constructor thing is still open:

"...is telling you that there is no BigInteger constructor that takes a BigInteger as an argument. (In fact, if you already have a BigInteger...?)"

I would really appreciate a short example of constructor that takes BigInteger as an argument. My brains are just jamming after thinking this thing...
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Scanner's method nextBigInteger() returns a reference to a BigInteger object, so you don't need to construct a new one. Just assign it...

BigInteger luku1 = nappaimisto.nextBigInteger();

If you really wanted to construct a BigInteger from a BigInteger, then I suppose you could convert to a String for the constructor...

...new BigInteger(nappaimisto.nextBigInteger().toString();

But this probably falls into the Bad Idea category.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BigInteger and multiplying