Sheriffo Ceesay
Sheriffo Ceesay

Reputation: 23

Using BigIntegers as a condition in while loop in Java

I want to use a big integer value in my while loop condition but this is not working.

import java.util.Scanner;
import java.util.StringTokenizer;
public class NiceProbelm2 {
public static void main(String[]args){
    Scanner input = new Scanner(System.in);
    String number = input.nextLine();
    StringTokenizer st = new StringTokenizer(number);
    BigInteger base = null;
    int power=0;
    while (st.hasMoreTokens()) {
          String token1 = st.nextToken();
          String token2 = st.nextToken();
          base = new BigInteger(token1);
          power = Integer.parseInt(token2);


        }

        BigInteger result = base.pow(power);
        //long fresult = (long)result;
        BigInteger div = new BigInteger("10");
        System.out.println(result);
        BigInteger sum=null;
     //NOt working while(result.compareTo(new BigInteger("0")) > 10 )
                    {
        BigInteger digit = result.mod(div);
        result = result.divide(div);
        sum = sum.add(digit);

    }
     System.out.println(sum);
}

}

Upvotes: 2

Views: 5656

Answers (4)

M. Jessup
M. Jessup

Reputation: 8222

I think you problem with the previous solutions were you had a null pointer that was being thrown because your accumulator was not initialized. The following code produces the result you are looking for:

        BigInteger result = base.pow(power);
        BigInteger div = new BigInteger("10");
        System.out.println(result);
//The following line is different, initialize the sum before using it.
        BigInteger sum = BigInteger.ZERO;
        while(!BigInteger.ZERO.equals(result)) {
            BigInteger digit = result.mod(div);
            result = result.divide(div);
            sum = sum.add(digit);
        }
        System.out.println(sum);

I also wanted to point out that BigInteger has a method that provides both the quotient and remainder from a single division, which is more efficient than doing them both separately, and there is a "valueOf" method for BigInteger so you can use a numeric literal instead of a string:

    BigInteger result = base.pow(power);
    BigInteger div = BigInteger.valueOf(10);
    System.out.println(result);

    //Initialize the sum to zero
    BigInteger sum = BigInteger.ZERO;        
    //While the result has a non-zero decimal digit
    while(!BigInteger.ZERO.equals(result)) {
        //this divides by ten (first element),
        //and calculates the remainder (second element)
        BigInteger[] lastDigit = result.divideAndRemainder(div);
        result = lastDigit[0];
        sum = sum.add(lastDigit[1]);

    }
    System.out.println(sum);

Upvotes: 1

Gabe
Gabe

Reputation: 86768

It looks like you want to do while (result != 0) which you would write like
while (result.compareTo(BigInteger.ZERO) != 0)

Also, you need to initialize sum to BigInteger.ZERO.

Upvotes: 0

frm
frm

Reputation: 3486

You have to use this condition:

result.compareTo(BigInteger.ZERO) > 0

Upvotes: 1

Joachim Sauer
Joachim Sauer

Reputation: 308149

You should never compare the return value of compareTo() to anything other than 0. (BigInteger.compareTo() is a bit more specific, but the same rule still applies)

You can check if it's greater than 0, less than 0 or equal to 0. Only those 3 pieces of information are actually relevant. The actual value (if it returns 1 or 10 or 100) doesn't matter.

Upvotes: 7

Related Questions