aspose file tools
The moose likes Beginning Java and the fly likes a little BigInteger problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "a little BigInteger problem" Watch "a little BigInteger problem" New topic
Author

a little BigInteger problem

James Jennings
Greenhorn

Joined: Mar 03, 2005
Posts: 25
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

Joined: Sep 28, 2004
Posts: 16695
    
  19

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

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.


[Jess in Action][AskingGoodQuestions]
bparanj
Greenhorn

Joined: May 27, 2002
Posts: 27
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);
}
}


Moderator : <a href="http://groups.yahoo.com/group/OOAD_UML/" target="_blank" rel="nofollow">http://groups.yahoo.com/group/OOAD_UML/</a> <br />Home : <a href="http://www.zepho.com" target="_blank" rel="nofollow">http://www.zepho.com</a>
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

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

Joined: Mar 03, 2005
Posts: 25
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
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
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
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

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

Joined: Jul 08, 2003
Posts: 24057
    
  13

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!"
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: a little BigInteger problem
 
Similar Threads
Wrapper
It compiled but couldn't run,help me make it run
BigInteger and multiplying
Range class
What' s wrong with this code?