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).
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
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
Joined: Mar 03, 2005
Posts: 25
posted
0
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
Joined: Jun 07, 2003
Posts: 88
posted
0
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
Joined: Mar 03, 2005
Posts: 25
posted
0
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:
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
Joined: Jun 07, 2003
Posts: 88
posted
0
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
Marshal