V��r� Tunnus

Greenhorn

Posts: 8

posted 11 years ago

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.

---------------------------------------------------------------------------

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.

posted 11 years ago

Welcome to JavaRanch!

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

...is telling you that you cannot call the multiply method from the

BigInteger tulo = luku1.multiply(luku2);

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

Posts: 8

posted 11 years ago

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...

"...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...

posted 11 years ago

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

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

But this probably falls into the Bad Idea category.

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.

*~Joe Strummer*

sscce.org

Don't get me started about those stupid light bulbs. |