6

So I want to check the number that is beyond the range of Long Datatype in Java. I have written some code but it does not go beyond the range of Long . What if I have given the number more than range of Long and it is supposed to be print that it is beyond long range or something. here is my code :

import java.math.BigInteger;
import java.util.Scanner;

public class Solution {

    public void check(String data) {
        System.out.println(data + " can be fitted in:");
        Long bInt = Long.parseLong(data);
        if (bInt >= Byte.MIN_VALUE && bInt <= Byte.MAX_VALUE) {
            System.out.println("* byte");
        }
        if (bInt >= Short.MIN_VALUE && bInt <= Short.MAX_VALUE) {
            System.out.println("* short ");
        }
        if (bInt >= Integer.MIN_VALUE && bInt <= Integer.MAX_VALUE) {
            System.out.println("* int ");
        }
        if (bInt >= Long.MIN_VALUE && bInt <= Long.MAX_VALUE) {
            System.out.println("* long ");
        }
    }

    public static void main(String args[]) {
        Solution solution = new Solution();
        Scanner sc = new Scanner(System.in);
        int data = Integer.parseInt(sc.nextLine());
        String[] array = new String[data];

        Scanner sc1 = new Scanner(System.in);

        for (int i = 0; i < data; i++) {
            array[i] = sc1.nextLine();
        }

        for (int j = 0; j < array.length; j++) {
            solution.check(array[j]);
        }
    }
}

with the help of Eran i changed the code slightly and it works.

Updated code :

import java.math.BigInteger;
import java.util.Scanner;

/**
 * 
 * @author pez
 */
public class Solution {

    public void check(String data) {
        try {
            Long bInt = Long.parseLong(data);
            System.out.println(data + " can be fitted in:");
            if (bInt >= Byte.MIN_VALUE && bInt <= Byte.MAX_VALUE) {
                System.out.println("* byte");
            }
            if (bInt >= Short.MIN_VALUE && bInt <= Short.MAX_VALUE) {
                System.out.println("* short ");
            }
            if (bInt >= Integer.MIN_VALUE && bInt <= Integer.MAX_VALUE) {
                System.out.println("* int ");
            }
            if (bInt >= Long.MIN_VALUE && bInt <= Long.MAX_VALUE) {
                System.out.println("* long ");
            }
        } catch (NumberFormatException e) {
            System.out.println(data + " can't be fitted anywhere beyond long ");
        }
    }

    public static void main(String args[]) {
        Solution solution = new Solution();
        Scanner sc = new Scanner(System.in);
        int data = Integer.parseInt(sc.nextLine());
        String[] array = new String[data];

        Scanner sc1 = new Scanner(System.in);

        for (int i = 0; i < data; i++) {
            array[i] = sc1.nextLine();
        }

        for (int j = 0; j < array.length; j++) {
            solution.check(array[j]);
        }
    }
}
Naman Gala
  • 4,670
  • 1
  • 21
  • 55
user2675040
  • 79
  • 2
  • 7

4 Answers4

11

Long.parseLong throws a NumberFormatException if you pass to it a number that can't be parsed as long. You can catch that exception and try to parse it as a BigInteger. If you succeed, you'll know that the input is a valid number that simply doesn't fit as long.

For example :

    try {
      System.out.println("Valid Long: " + Long.parseLong("1234567890123456789012345"));
    } catch (NumberFormatException n) {
      System.out.println("Valid Big Integer: " + new BigInteger ("1234567890123456789012345").toString());
    }

This prints :

Valid Big Integer: 1234567890123456789012345

which means 1234567890123456789012345 is not a valid long.

If the BigInteger constructor also throws a NumberFormatException, you know that the input can't be parsed as a valid number (this will happen, for example, if the parsed String contains non-numeric characters).

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Thank you very much i added try and catch and now it works without any errors. – user2675040 Jun 08 '15 at 05:50
  • Double.parseDouble("179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858371"); wont works as it not throws any exception – Nagappa L M Nov 09 '22 at 12:14
1

when number exceed Long.MAX_VALUE, Long.parseLong throws a NumberFormatException issue you can assign variable as BigInteger and compare with Long.MAX_VALUE.

public void check(String data){
data = "1234567890123456789012345";
BigInteger bg = new BigInteger (data);

try{
 //check number is in long.MAX_VALUE
 System.out.println(bg.compareTo(new BigInteger (String.valueOf(Long.MAX_VALUE))));
}catch (NumberFormatException n) {
    System.out.println("not a valid number");
}}
Community
  • 1
  • 1
0

Use BigInteger and compare it with Long.MAX_VALUE . Practically BigInteger doesn't have any limit.

Kalyan Chavali
  • 1,330
  • 8
  • 24
  • Theoretically doesn't have limit, practically is limited by memory given to JVM. Even better [BigInteger bound](http://stackoverflow.com/a/12693333/4327527) – Milkmaid Jun 08 '15 at 05:45
0

I used as

public  static boolean isNumberLargerThan(Number a, double maxValue) {
        return new BigDecimal(String.valueOf(a.toString())).compareTo(new BigDecimal(maxValue))==1 ? true : false;
    } 

On a note this way is expensive

Nagappa L M
  • 1,452
  • 4
  • 20
  • 33