9999999999
BigInteger
constructor
There are several constructors for BigInteger
.
For now, the most convenient constructor is
BigInteger( String str )
Construct aBigInteger
based on the digits instr
.
str
can start with a plus or minus sign, but all other characters must be digits (no white space.) If there is an illegal character, aNumberFormatException
exception is thrown.
Here is another program that uses
the String
method trim()
to remove leading and trailing whitespace.
import java.math.BigInteger; import java.util.Scanner; class UserInput { public static void main ( String[] args ) { Scanner scan = new Scanner( System.in ); String input; BigInteger a, b, sum; System.out.print("First integer: "); input = scan.next(); a = new BigInteger( input.trim() ); System.out.print("Second integer: "); input = scan.next(); b = new BigInteger( input.trim() ); sum = a.add( b ); System.out.println("Sum: " + sum); } }
C:> javac UserInput.java C:> java UserInput First integer: 12345678987654321 Second integer: 87654321012345678 Sum: 99999999999999999
Will the following constructor work?
int x = 492; BigInteger value = new BigInteger( x );