Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

BigInteger and multiplying

 
V��r� Tunnus
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
V��r� Tunnus
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic