-1

When i try to compare BigInteger and int:

 BigInteger balance = new BigInteger(out_str.substring(186, 201).trim());
 if (!balance.equals(0)) {...}

I get:

equals () between objects of inconvertible types 'int' and 'BigInteger'

Hille
  • 2,123
  • 22
  • 39
bardir16
  • 13
  • 5

2 Answers2

5

Use

if (!balance.equals(BigInteger.ZERO)) {
    ...
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
Mustapha Belmokhtar
  • 1,231
  • 8
  • 21
4

int and BigInteger don't share any class as int is a primitive type.

You can only compare stuff which has at least something in common, like Object. So a comparison like BigInteger with the wrapper class Integer would compile, but the result would be false since an Integer is no BigInteger. You will need to transform your int to a BigInteger for comparison.

For 0 there is the constant BigInteger.ZERO (documentation):

if (!balance.equals(BigInteger.ZERO)) {
    ...
}
Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • What does "you can only compare stuff which has at least something in common" mean? – Kayaman Nov 20 '17 at 15:36
  • @Kayaman If you look at the class hierarchy of both objects, there must be a common type. This is true for all *objects* since they always implicitly are of type `Object` at least. However **primitive types** are not of type `Object`, thus they are not comparable. But yeah, my formulation is **vague**. Feel free to edit the answer if you have a better formulation :) – Zabuzard Nov 20 '17 at 18:37
  • There's no reason why you couldn't create classes `A` and `B` that don't share a superclass, but can be compared with `equals()`. A common superclass is not a requirement, but it can be advantageous (such as with `Number`). – Kayaman Nov 20 '17 at 18:44