Java Datatypes Solution


Problem Overview

Original Problem: Java Datatypes | Hackerrank

Difficulty: Easy

Category: Data Types / Input Validation

Description: We are given multiple numeric inputs, and our task is to determine which of Java's integer-based primitive datatypes — byte, short, int, and long — can safely hold each number. Print the compatible data types in order of increasing size. If a number doesn’t fit into any of them, indicate that it cannot be stored.

Understanding the Problem

Java offers four main primitive types for storing whole numbers:

  • byte: 8-bit signed integer

  • short: 16-bit signed integer

  • int: 32-bit signed integer

  • long: 64-bit signed integer

We are given a number of test cases and for each case, we receive a single number. We need to determine which (if any) of the above types can store the number.

Input:

The first line stores the number of inputs to test (t). The next t lines each contain a number (n)

Output:

For each n, print:

  • "n can be fitted in:" followed by one or more lines like "* dataType"

  • If n doesn’t fit into any of the four types, print "n can't be fitted anywhere."

Initial Reasoning

The original problem gives us template code to iterate through each test case and the structure to test if the number is a byte. Given the number can be too large to store in a long, we are also provided with a try-catch block which we can use to handle the case where the number doesn’t fit into any. From her,e we can use a similar structure of if statements, checking to see if the number is within the range of each datatype.

Pseudocode

// Main function:
Read in the number of test cases
For each test case
	Try to parse the number as a long
	If the number is parsed successfully
		Print “n can be fitten in”
		If the number can be stored in a byte
			print “* byte”
		If the number can be stored in an int
			print “* int”
		If the number can be stored in a short
			print”* short”
		Print “* long”
	If the number cannot be parsed
		Print “n can't be fitted anywhere”

Solution Code

// Note this is one of many possible solutions
import java.util.Scanner;

public class JavaDatatypes {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int numCases = scanner.nextInt(); // Read number of test cases

        for (int i = 0; i < numCases; i++) {
            try {
                // Read the input number as a long
                long number = scanner.nextLong();
                System.out.println(number + " can be fitted in:");

                // Check against each data type range
                if (number >= Byte.MIN_VALUE && number <= Byte.MAX_VALUE)
                    System.out.println("* byte");

                if (number >= Short.MIN_VALUE && number <= Short.MAX_VALUE)
                    System.out.println("* short");

                if (number >= Integer.MIN_VALUE && number <= Integer.MAX_VALUE)
                    System.out.println("* int");

                //if (number >= Long.MIN_VALUE && number <= Long.MAX_VALUE)
                    System.out.println("* long");

            } catch (Exception e) {
                // If parsing fails (number too large or invalid), print this
                String badInput = scanner.next(); // Read the invalid token
                System.out.println(badInput + " can't be fitted anywhere.");
            }
        }
        scanner.close();
    }
}

Commenting on Alterations

While we have done our best to maintain similarity to the template code provided in the original problem, the following changes have been made to follow best practices:

  • Renamed variables:

    Using more descriptive names improves the clarity of the code and makes it easier for anyone reading the code to understand what each variable represents.