j.add(BigInteger.valueOf(1))
does not change j
. It returns a new BigInteger
whose value is one more than j
, but you're discarding it. Try j = j.add(BigInteger.ONE)
[this constant is provided for you so you don't have to type valueOf(1)
].
In fact, BigInteger
(like String
) is an immutable type, which means there are no methods that change the contents of a BigInteger
object, only methods that return new BigInteger
objects.