Java Datatypes

Sort by

recency

|

1521 Discussions

|

  • + 0 comments

    import java.util.Scanner;

    public class Test { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt();

        for (int i = 0; i < t; i++) {
            try {
                long x = sc.nextLong();
                System.out.println(x + " can be fitted in:");
                if (x >= -128 && x <= 127)
                    System.out.println("* byte");
                if (x >= -32768 && x <= 32767)
                    System.out.println("* short");
                if (x >= -2147483648 && x <= 2147483647)
                    System.out.println("* int");
                System.out.println("* long");
            } catch (Exception e) {
                System.out.println(sc.next() + " can't be fitted anywhere.");
            }
        }
        sc.close();
    }
    

    }

  • + 0 comments

    instead of passing the hardcoded range, you can use the wrapper class to set the minimum and maximum value for all the conditions.

    import java.util.*;
    class Solution {
        public static void main(String[] argh) {
            Scanner scanner = new Scanner(System.in);
            int t = scanner.nextInt();
            for (int i = 0; i < t; i++) {
                try {
                    long x = scanner.nextLong();
                    System.out.println(x + " can be fitted in:");
                    if (x >= Byte.MIN_VALUE && x <= Byte.MAX_VALUE) {
                        System.out.println("* byte");
                    }
                    if (x >= Short.MIN_VALUE && x <= Short.MAX_VALUE) {
                        System.out.println("* short");
                    }
                    if (x >= Integer.MIN_VALUE && x <= Integer.MAX_VALUE) {
                        System.out.println("* int");
                    }
                    if (x >= Long.MIN_VALUE && x <= Long.MAX_VALUE) {
                        System.out.println("* long");
                    }
                } catch (Exception e) {
                    System.out.println(scanner.next() + " can't be fitted anywhere.");
                }
            }
            scanner.close();
        }
    }
    
  • + 0 comments

    import java.util.Scanner;

    class Solution { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt();

        for (int i = 0; i < t; i++) {
            try {
                long x = sc.nextLong();
                System.out.println(x + " can be fitted in:");
                if (x >= -128 && x <= 127)
                    System.out.println("* byte");
                if (x >= -32768 && x <= 32767)
                    System.out.println("* short");
                if (x >= -2147483648 && x <= 2147483647)
                    System.out.println("* int");
                System.out.println("* long"); // `x` can always fit in `long`
            } catch (Exception e) {
                System.out.println(sc.next() + " can't be fitted anywhere.");
            }
        }
        sc.close();
    }
    

    }

  • + 0 comments
    import java.util.*;
    import java.io.*;
    
    
    class Solution{
        public static void main(String []argh)
        {
            Scanner sc = new Scanner(System.in);
            int t=sc.nextInt();
            for(int i=0;i<t;i++)
            {
                try
                {
                    long x=sc.nextLong();
                    System.out.println(x+" can be fitted in:");
                    if(x>=-128 && x<=127)System.out.println("* byte");
                    if(x>=-32768 && x<=32767) System.out.println("* short");
                    if(x>=-2147483648 && x<=2147483647) System.out.println("* int");
                    if(x>=-9223372036854775808L && x<=9223372036854775807L) System.out.println("* long");
                }
                catch(Exception e)
                {
                    System.out.println(sc.next()+" can't be fitted anywhere.");
                }
    
        }
    }
    

    }

  • + 0 comments

    > If You Got Test Case Failed. Only this Solution Is Perfect.

    import java.util.*;
    import java.io.*;
    
    
    
    class Solution{
        public static void main(String []argh)
        {
    
    
    
            Scanner sc = new Scanner(System.in);
            int t=sc.nextInt();
    
            for(int i=0;i<t;i++)
            {
    
                try
                {
                    long x=sc.nextLong();
                    System.out.println(x+" can be fitted in:");
                    if(x>=-128 && x<=127)System.out.println("* byte");
                    if(x>=Short.MIN_VALUE && x<=Short.MAX_VALUE)System.out.println("* short");
                    if(x>=Integer.MIN_VALUE && x<=Integer.MAX_VALUE)System.out.println("* int");
                    if(x>=Long.MIN_VALUE && x<=Long.MAX_VALUE)System.out.println("* long");
                    
                }
                catch(Exception e)
                {
                    System.out.println(sc.next()+" can't be fitted anywhere.");
                }
    
            }
        }
    }