如何在Java中检查超出范围之外的范围?

use*_*040 5 java types

所以我想检查Java中Long数据类型范围之外的数字.我已经编写了一些代码,但它不会超出Long的范围.如果我给出的数字超过Long的范围并且它应该是超出长距离或其他东西的印刷品怎么办?这是我的代码:

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]);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Eran我的帮助下,我稍微改变了代码,它的工作原理.

更新的代码:

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]);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Era*_*ran 9

Long.parseLong抛出一个NumberFormatException如果你向它传递一个无法解析的数字.您可以捕获该异常并尝试将其解析为BigInteger.如果你成功了,你就会知道输入是一个有效的数字,只是不合适.

例如 :

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

这打印:

Valid Big Integer: 1234567890123456789012345
Run Code Online (Sandbox Code Playgroud)

这意味着1234567890123456789012345不是有效的长.

如果BigInteger构造函数也抛出a NumberFormatException,则您知道输入无法解析为有效数字(例如,如果解析String包含非数字字符,则会发生这种情况).