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: http://www.java-forums.org/new-java/...tml#post111732

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);
}

return new BigRational
(this.numerator.multiply(r.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;
}
}

}```  Reply With Quote

2. Senior Member Join Date
Mar 2010
Posts
266
Rep Power
11

## 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 un-intended whitespace in your string. So just in case, do this:

// acquire String str somewhere
str = str.trim (); // strips the whitespace
new BigInteger (str);  Reply With Quote

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();
den.trim();
BigInteger numerator = new BigInteger(num);
BigInteger denominator = new BigInteger(den);
BigRational one = new BigRational(numerator,denominator);```
and now everything seems to work nicely
thanks  Reply With Quote

4. ##  Originally Posted by sonny 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
Or perhaps not enough: Ballmer Peak

Looks like your programming is coming along nicely. Keep it up.  Reply With Quote

5. ##  Originally Posted by Fubarable Or perhaps not enough: Ballmer Peak

Looks like your programming is coming along nicely. Keep it up.
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));
}
}```  Reply With Quote

biginteger, syntax 