28

I want to take Input from the user as Big-Integer and manipulate it into a For loop

BigInteger i;
for(BigInteger i=0; i<=100000; i++) {
    System.out.println(i);
}

But it won't work

can any body help me.

Alex
  • 9,891
  • 11
  • 53
  • 87
Sanjeev
  • 1,087
  • 8
  • 18
  • 27
  • Reformatted code; please revert if incorrect. – trashgod Jun 11 '10 at 15:47
  • 3
    it looks so odd: it grows strangely ( i = i + i - 1 ) ... moreover it is not <= 1 at the beginning so the loop is not executed at all... – ShinTakezou Jun 11 '10 at 15:53
  • BigInteger i is getting declared twice; I'd think this wouldn't compile. – BlairHippo Jun 11 '10 at 15:59
  • I purposedly didn't address the weirdness of your snippet (others have done that) but instead try to answer a more general question regarding `BigInteger` usage. If you can explain what you're tyring to do, I can see if there's anything we may have missed. – polygenelubricants Jun 11 '10 at 16:17

5 Answers5

57

You use these syntax instead:

BigInteger i = BigInteger.valueOf(100000L);  // long i = 100000L;
i.compareTo(BigInteger.ONE) > 0              // i > 1
i = i.subtract(BigInteger.ONE)               // i = i - 1

So here's an example of putting it together:

    for (BigInteger bi = BigInteger.valueOf(5);
            bi.compareTo(BigInteger.ZERO) > 0;
            bi = bi.subtract(BigInteger.ONE)) {

        System.out.println(bi);
    }
    // prints "5", "4", "3", "2", "1"

Note that using BigInteger as a loop index is highly atypical. long is usually enough for this purpose.

API links


The compareTo idiom

From the documentation:

This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, >=, !=, <=). The suggested idiom for performing these comparisons is: (x.compareTo(y)<op>0), where <op> is one of the six comparison operators.

In other words, given BigInteger x, y, these are the comparison idioms:

x.compareTo(y) <  0     // x <  y
x.compareTo(y) <= 0     // x <= y
x.compareTo(y) != 0     // x != y
x.compareTo(y) == 0     // x == y
x.compareTo(y) >  0     // x >  y
x.compareTo(y) >= 0     // x >= y

This is not specific to BigInteger; this is applicable to any Comparable<T> in general.


Note on immutability

BigInteger, like String, is an immutable object. Beginners tend to make the following mistake:

String s = "  hello  ";
s.trim(); // doesn't "work"!!!

BigInteger bi = BigInteger.valueOf(5);
bi.add(BigInteger.ONE); // doesn't "work"!!!

Since they're immutable, these methods don't mutate the objects they're invoked on, but instead return new objects, the results of those operations. Thus, the correct usage is something like:

s = s.trim();
bi = bi.add(BigInteger.ONE);
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
  • So this will iterate from 5 to 1. But what if I want to write the for loop which iterates from 0 to 4 containing five values. Can I do that ? – Lokesh Pandey Oct 13 '16 at 17:29
3

Well, first of all, you have two variables called "i".

Second, where's the user input?

Third, i=i+i unboxes i into a primitive value, possibly overflowing it, and boxes the result in a new object (that is, if the statement even compiles, which I haven't checked).

Fourth, i=i+i can be written as i = i.multiply(BigInteger.valueof(2)).

Fifth, the loop is never run, because 100000 <= 1 is false.

Justin K
  • 2,664
  • 1
  • 19
  • 16
1

I think this code should work

public static void main(String[] args) {
    BigInteger bigI = new BigInteger("10000000");
    BigInteger one = new BigInteger("1");

    for (; bigI.compareTo(one) == 0; bigI.subtract(one)) {
       bigI = bigI.add(one);
    }
}
vodkhang
  • 18,639
  • 11
  • 76
  • 110
1

This could work

BigInteger i;
        for(i = BigInteger.valueOf(0);i.compareTo(new BigInteger("100000")) 
            <=0;i=i.add(BigInteger.ONE))
        {
            System.out.println(i);
        }

(or)

for(BigInteger i = BigInteger.valueOf(0);i.compareTo(new BigInteger("100000")) 
            <=0;i=i.add(BigInteger.ONE))
        {
            System.out.println(i);
        }

Note that don't declare BigInteger twice.

0

This is for running the loop in ascending order:

BigInteger endValue = BigInteger.valueOf(100000L);

for (BigInteger i = BigInteger.ZERO; i.compareTo(endValue) != 0; i = i.add(BigInteger.ONE)) {
   //... your logic inside the loop
}
jprakash
  • 1
  • 1