2

I want to use BigInteger in if statement. But BigInteger needs to convert to int for compare with integer data type. how to convert BigInteger as a int.

This is my buggy code.

    Scanner in = new Scanner(System.in);
    BigInteger input = in.nextBigInteger();
     
    if(input <= 127 )
    {
       //code 
       
    }
Rizen_137
  • 21
  • 4
  • 1
    A decent language like C++ would allow you to write the code as you have. If I recall correctly there is a `compareTo()` function that you have to use. Have an upvote for sympathy. – Bathsheba Apr 06 '22 at 10:25
  • 1
    `if(input.compareTo(BigInteger.valueOf(127)) <= 0)` – Eritrean Apr 06 '22 at 10:31
  • 2
    @Eritrean: `<= 0` ? Or even `< 1` which would make the code even harder to read ;-) – Bathsheba Apr 06 '22 at 10:32
  • 1
    [`BigInteger.compareTo(java.math.BigInteger)`](https://docs.oracle.com/javase/8/docs/api/java/math/BigInteger.html#compareTo-java.math.BigInteger-) – seenukarthi Apr 06 '22 at 10:32

0 Answers0