Results 1 to 5 of 5
 04132010, 02:55 AM #1
BigIntegers syntax.. How to read in from console program
How to i read in a BigInteger from the console prgram??
The exercise was to rewrite a rational number class using BigIntegers ( the original code which uses ints was provided in my study text and is posted here: trying to understand this rational numbers class
after consulting javadoc and other stuff on google I think i have achieved the goal (at least eclipse shows no compile errors) however i am having a mental block or something, because i cant work out how to test my code because i cannot read in a biginteger.
i have tried various things using scrapbook and a console program . I am assuming that I have to read in a string and convert it to a biginteger.
I arrived at this notion because of entry number 5 in the constructor summary information at javadoc states:
BigInteger(String val)
Translates the decimal String representation of a BigInteger into a BigInteger.
and i am assuming this is the way I must do it,,
it has occured to me however that perhaps my code (despite having no compile errors) is a pile of junk! so ive posted it below.
also there is something about throwing exceptions which i may not have done in the conventional way, i am not concerned with this too much at the moment, but any suggestions on throwing exceptions will be gratefully accepted.
BigRational code:
Java Code:import java.math.BigInteger; import acm.util.ErrorException; public class BigRational { private BigInteger numerator; private BigInteger denominator; public BigRational(){ this(BigInteger.ZERO); } public BigRational(BigInteger numerator){ this(numerator,BigInteger.ONE); } public BigRational(BigInteger numerator, BigInteger denominator) { if (BigInteger.ZERO == denominator) throw new ErrorException("Cannot divide by zero."); BigInteger gcd = numerator.abs().gcd(denominator.abs()); this.numerator = numerator.divide(gcd); this.denominator = denominator.divide(gcd); } public BigRational add(BigRational r) { return new BigRational (this.numerator.multiply(r.denominator) .add(r.numerator.multiply(this.denominator)), this.denominator.multiply(r.denominator)); } public BigRational subtract(BigRational r) { return new BigRational(this.numerator.multiply(r.denominator) .subtract(r.numerator).multiply(this.denominator), this.denominator.multiply(r.denominator)); } public BigRational multiply(BigRational r) { return new BigRational(this.numerator.multiply(r.numerator), this.denominator.multiply(r.denominator)); } public BigRational divide(BigRational r) { return new BigRational(this.numerator.multiply(r.denominator), this.denominator.multiply(r.numerator)); } public String toString() { if (denominator == BigInteger.ONE) { return "" + numerator; } else { return numerator + "/" + denominator; } } }
:p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts
 04132010, 03:21 AM #2Senior Member
 Join Date
 Mar 2010
 Posts
 266
 Rep Power
 5
your code looks good.
Instead of throwing ErrorException consider IllegalArgumentException
as far as parsing string into BigInteger, you should indeed be able to use
String str = "123";
new BigInteger (str);
when you're reading from console, one thing to watch out for is unintended whitespace in your string. So just in case, do this:
// acquire String str somewhere
str = str.trim (); // strips the whitespace
new BigInteger (str);
 04132010, 01:21 PM #3
thanks!
Thanks mate,
it might have helped if i had imported java.math into my scrapbook :o
perhaps i would have remembered had i not had a few beers! LOL
Java Code:String num = readLine(" Numerator: "); num.trim(); String den = readLine("Denominator: "); den.trim(); BigInteger numerator = new BigInteger(num); BigInteger denominator = new BigInteger(den); BigRational one = new BigRational(numerator,denominator);
thanks:p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts

 04142010, 05:01 AM #5
many thanks
hopefully its coming along as well as my capacity for beer!
Java Code:private void gotSomeTime2Kill(int beersInFridge, boolean WifeAndKidsInBed); while (beersInFridge > 0 && wifeAndKidsInBed){ consumeOneBeer(); beersInFridge; beerConsumed++ doSomeMoreCode(); if(beerConsumed > BallmerPeak) postSomeDaftHelpRequest(); else { gotSomeTimeToKill(beersInFridge,(true)); } }
:p I still have my "L" plates on...... directions and explanations are far more help than blaring your Horn! :p Watching:CS106a on YouTube \Reading The Art & Science of Java by Eric S Roberts
Similar Threads

Ready To Program, Images in HSA Console?
By habbah in forum New To JavaReplies: 2Last Post: 01172010, 04:50 AM 
How to read input from console
By Java Tip in forum java.ioReplies: 0Last Post: 04162008, 11:57 PM 
How to read input from the console
By Java Tip in forum java.ioReplies: 0Last Post: 04062008, 08:41 PM 
How to read a Web Page through java program
By Java Tip in forum java.netReplies: 0Last Post: 04042008, 03:37 PM 
Read from console (Scanner Class)
By hey in forum New To JavaReplies: 10Last Post: 12112007, 11:11 PM
Bookmarks