go to previous page   go to home page   go to next page highlighting

Answer:

9999999999


BigInteger constructor

There are several constructors for BigInteger. For now, the most convenient constructor is

BigInteger( String str )

Construct a BigInteger based on the digits in str.
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, a NumberFormatException 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


QUESTION 7:

Will the following constructor work?

int x = 492;

BigInteger value = new BigInteger( x );

go to previous page   go to home page   go to next page