Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check the range that exists beyond Long range in Java?

Tags:

java

types

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]);
        }
    }
}
like image 583
user2675040 Avatar asked Jun 08 '15 05:06

user2675040


2 Answers

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).

like image 113
Eran Avatar answered Oct 01 '22 23:10

Eran


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");
}}
like image 35
Paraneetharan Saravanaperumal Avatar answered Oct 02 '22 00:10

Paraneetharan Saravanaperumal