Java Datatypes

  • + 0 comments

    I was a bit confused and decided to use BigInteger and comparisons. In any case, this task requires more tools than just primitive data types. Here's my solution.

    public static void main(String[] args) {
            var scanner = new Scanner(System.in);
            int cases =  scanner.nextInt();
            String[] values = new String[cases];
            scanner.nextLine();
            for (int i = 0; i < cases; i++) {
                    values[i] = scanner.nextLine();
            }
    
            for (int i = 0; i < cases; i++) {
                    BigInteger bi = new BigInteger(values[i]);
                    if (bi.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0 || bi.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) < 0) {
                            System.out.println(values[i] + " can't be fitted anywhere.");
                    } else {
                            System.out.println(values[i] + " can be fitted in:");
                            if (bi.compareTo(BigInteger.valueOf(Byte.MAX_VALUE)) <= 0 && bi.compareTo(BigInteger.valueOf(Byte.MIN_VALUE)) >= 0) {
                                    System.out.println("* byte");
                            }
                            if (bi.compareTo(BigInteger.valueOf(Short.MAX_VALUE)) <= 0 && bi.compareTo(BigInteger.valueOf(Short.MIN_VALUE)) >= 0) {
                                    System.out.println("* short");
                            }
                            if (bi.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) <= 0  && bi.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) >= 0) {
                                    System.out.println("* int");
                            }
                            System.out.println("* long");
                    }
            }
            scanner.close();
    }