• Post Reply Bookmark Topic Watch Topic
  • New Topic

a little BigInteger problem  RSS feed

 
James Jennings
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would appreciate help on this problem. The code below will not compile.

import java.math.*;

class BigIntTest
{
private String sBigI = new String("12345");
public BigInteger aBigInt = BigInteger(sBigI); // line 6
}

I am getting this error message:

C:\Documents and Settings\James Jennings\My Documents\BigIntTest.java:6: cannot find symbol
symbol : method BigInteger(java.lang.String)
location: class BigIntTest
public BigInteger aBigInt = BigInteger(sBigI);
^
1 error

Tool completed with exit code 1

The java API says this:
public BigInteger(String val); // decimal String representation of BigInteger.

Description
Translates the decimal String representation of a BigInteger into a BigInteger. The String representation consists of an optional minus sign followed by a sequence of one or more decimal digits. The character-to-digit mapping is provided by Character.digit. The String may not contain any extraneous characters (whitespace, for example).
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler is looking for a BigInteger() method in the BigIntTest class. I think you left out the "new" keyword.

public BigInteger aBigInt = new BigInteger(sBigI);

Henry
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi James,

You left out the "new" in "new BigInteger(sBigI)". As a result, the compiler is looking for a method in your class named "BigInteger", and of course there isn't one. So put that "new" in there, and you'll be all set.

Incidentally, the "new String()" you're doing is not needed, and in fact it's a bad habit to get into. Just saying

private String sBigI = "12345";

is fine.
 
bparanj
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because you forgot the new operator before the BigInteger. Here is the code that works:

package test;

import java.math.*;

class BigIntTest
{
private String sBigI = new String("12345");
public BigInteger aBigInt = new BigInteger(sBigI); // line 6

public static void main(String args[]) {
BigIntTest bigIntTest = new BigIntTest();
System.out.println("Test..." + bigIntTest.aBigInt);
}
}
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bparanj,

Welcome to JavaRanch!

A bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. "Handles" and joke names aren't enough. You can change your display name here. Thanks!
 
James Jennings
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks fellows. What a quick response! javaranch is better than having a pair programmer sitting beside me. The pgm compiles now.

Ernest, I understand your comment about the string. I put the extra line in to make sure I had a string. I was trying everything I could think of.

(All the stupid mistakes I'm making, makes me think I'm getting too old for such demanding work.)
 
Mark Wuest
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
Incidentally, the "new String()" you're doing is not needed, and in fact it's a bad habit to get into. Just saying

private String sBigI = "12345";

is fine.


'Just curious as to why this is a bad habit. When I was first switching from C++ and disagreeing with the lectures about "operator overloading is evil" (we took a seminar from Sun back when Java was really new) and then saw this construct, I wanted to yell "Bad Form! Bad Form! Operator Overloading! Bad Form!".

Mark
 
James Jennings
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest I didn't understand. I reread you comment and now I do. I thought you were saying I should instantiate the BigInteger this way:

public BigInteger aBigInt = new BigInteger("12345");
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Wuest:
Just curious as to why this is a bad habit.


Hopefully I understand you correctly...

The "=" is not overloaded here. In C or C++ a "double quoted string" translates into a const char *, so saying something in C++ like



is translated behind your back into



In contrast, in Java, a quoted String literally is a String object. It's not a char[]; if it were, then you'd have a good point about overloading.

If you don't believe me, note that

char[] chars = "abc";

doesn't compile.

But in any case, if you write 'new String("foo")', you're making an unneeded copy of an already-existing, immutable String object. Except in exceedingly rare circumstances (and if you can't think of one, then you don't need to worry about it!) you never need to do that.
 
Mark Wuest
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ernest Friedman-Hill:
[QBBut in any case, if you write 'new String("foo")', you're making an unneeded copy of an already-existing, immutable String object. Except in exceedingly rare circumstances (and if you can't think of one, then you don't need to worry about it!) you never need to do that.[/QB]


Sorry I wasn't clear that I actually knew it wasn't "really" overloading, but just an exceptional way to instanciate an object.

c'mon, admit it it: it makes you wanna write:





Mark
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Wuest:

Integer foo = 7;


As it turns out, in Java 5 (Tiger), this is now perfectly valid Java code! Of course, again, it's not operator overloading: it's "autoboxing!"
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!