0

Maybe I'm misunderstanding due to my limited use of biginteger, but I have a program that needs to take user input for page size and virtual address and calculate offset and virtual page. I can do the calculation no problem, but when comparing itegers for the input, the necessary value of virtual address ( 4294967295)is out of range. I tried to declare this number as a big int but the same operators don't seem to work. Do I have to forget the zero and just find a way to say the input is less than or equal to this number? Here is the code:

public class VAddress {

public static void main(String args[]){

    BigInteger max = new BigInteger("4294967295"); 

    Scanner PAGEinput = new Scanner(System.in);
    Scanner ADDinput = new Scanner(System.in);

      System.out.println("Please Enter the system page size (between 512 and 16384):");
    int page = PAGEinput.nextInt();

    System.out.println("Please Enter the Virtual Address: ");
    int address = ADDinput.nextInt();

    if(page >= 512 && page <= 16384){
        if( address >= 0 && address <= max){

        }
       }



     }

 }
guitar138
  • 33
  • 1
  • 1
  • 7

2 Answers2

1
BigInteger address = new BigInteger(address);
if(page >= 512 && page <= 16384){
    if(address.compareTo(new BigInteger("0"))>=0 && address.compareTo(max)<=0){

    }
}
jst
  • 1,697
  • 1
  • 12
  • 13
0

You need to turn the integer address into a BigInteger as well so that you can compare it to max, see this question

Community
  • 1
  • 1
Moritz
  • 4,565
  • 2
  • 23
  • 21
  • So something similar to this: System.out.println("Please Enter the Virtual Address: "); BigInteger address = ADDinput.nextBigInteger(); – guitar138 Jun 05 '15 at 16:28
  • yup. Don't forget to cast the 0 as biginteger too if you go this route – Moritz Jun 05 '15 at 16:33