Java Datatypes

Sort by

recency

|

1581 Discussions

|

  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int n = Integer.parseInt(scanner.nextLine());
            for (int i = 0; i < n; i++) {
                String value = scanner.nextLine();
                try {
                    long currentNum = Long.parseLong(value);
                    System.out.printf("%s can be fitted in:%n", currentNum);
                    if (currentNum >= Byte.MIN_VALUE && currentNum <= Byte.MAX_VALUE) {
                        System.out.println("* byte");
                    }
                    if (currentNum >= Short.MIN_VALUE && currentNum <= Short.MAX_VALUE) {
                        System.out.println("* short");
                    }
                    if (currentNum >= Integer.MIN_VALUE && currentNum <= Integer.MAX_VALUE) {
                        System.out.println("* int");
                    }
                    if (currentNum >= Long.MIN_VALUE && currentNum <= Long.MAX_VALUE) {
                        System.out.println("* long");
                    }
                } catch (Exception e) {
                    System.out.printf("%s can't be fitted anywhere.%n", value);
                }
            }
            
            
            scanner.close();
        }
    }
    
  • + 1 comment

    || this is the easiest approach in java to solve this problem

    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\n* short\n* int\n* long");
                        
                    }else if(x>=-32768 && x<=32767){
                        System.out.println("* short\n* int\n* long");
                    }else if(x>=-2147483648 && x<=2147483647){
                        System.out.println("* int\n* long");
                    }else if(x>=-9223372036854775808l && x<=9223372036854775807l){
                        System.out.println("* long");
                    }
                    
                }
                catch(Exception e)
                {
                    System.out.println(sc.next()+" can't be fitted anywhere.");
                }
    
            }
        }
    }
    
  • + 0 comments

    import java.util.; import java.math.;

    class Solution { public static void main(String[] argh) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); // Consume newline

        for (int i = 0; i < t; i++) {
            String value = sc.nextLine();
            try {
                BigInteger x = new BigInteger(value);
    
                System.out.println(x + " can be fitted in:");
    
                if (x.compareTo(BigInteger.valueOf(Byte.MIN_VALUE)) >= 0 &&
                    x.compareTo(BigInteger.valueOf(Byte.MAX_VALUE)) <= 0)
                    System.out.println("* byte");
    
                if (x.compareTo(BigInteger.valueOf(Short.MIN_VALUE)) >= 0 &&
                    x.compareTo(BigInteger.valueOf(Short.MAX_VALUE)) <= 0)
                    System.out.println("* short");
    
                if (x.compareTo(BigInteger.valueOf(Integer.MIN_VALUE)) >= 0 &&
                    x.compareTo(BigInteger.valueOf(Integer.MAX_VALUE)) <= 0)
                    System.out.println("* int");
    
                if (x.compareTo(BigInteger.valueOf(Long.MIN_VALUE)) >= 0 &&
                    x.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) <= 0)
                    System.out.println("* long");
    
            } catch (Exception e) {
                System.out.println(value + " can't be fitted anywhere.");
            }
        }
    
        sc.close();
    }
    

    }

  • + 1 comment
    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>=-Math.pow(2, 31) && x<=Math.pow(2, 31)-1) System.out.println("* int");
                    if (x>=-Math.pow(2, 63) && x<=Math.pow(2, 63)-1) System.out.println("* long");
                } catch(Exception e) {
                    System.out.println(sc.next()+ " can't be fitted anywhere.");
                }
    
            }
        }
    }
    
  • + 2 comments
    try
         {
           long x=sc.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(sc.next()+" can't be fitted anywhere.");
         }