Java Datatypes

Sort by

recency

|

1613 Discussions

|

  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
    
        public static void main(String[] args) {
            Scanner sc=new Scanner(System.in);
            int test=sc.nextInt();
            while(test -- >0){
                String n=sc.next();
                boolean fit=false;
                try{
                    
                    byte b=Byte.parseByte(n);
                    if(!fit){
                        fit=true;
                        System.out.println(b+" can be fitted in:");
                    }
                    System.out.println("* byte");
                }
                catch(Exception e){}
                try{
                    
                    short b=Short.parseShort(n);
                    if(!fit){
                        fit=true;
                        System.out.println(b+" can be fitted in:");
                    }
                    System.out.println("* short");
                }
                catch(Exception e){}
                try{
                    
                    int b=Integer.parseInt(n);
                    if(!fit){
                        fit=true;
                        System.out.println(b+" can be fitted in:");
                    }
                    System.out.println("* int");
                }
                catch(Exception e){}
                try{
                    
                    long b=Long.parseLong(n);
                    if(!fit){
                        fit=true;
                        System.out.println(b+" can be fitted in:");
                    }
                    System.out.println("* long");
                }
                catch(Exception e){}
             if(!fit){
                System.out.println(n+" can't be fitted anywhere.");
             }   
            }
    }
    }
    
  • + 0 comments

    import java.io.; import java.util.;

    public class Solution {

    public static void main(String[] args) {
            Scanner scn = new Scanner(System.in);
    
            int t = scn.nextInt();
            while (t != 0) {
                    try {
                            long num = scn.nextLong();
                            System.out.println(num + " can be fitted in:");
                            if (num >= -128 && num <= 127) {
                                    System.out.println("* byte\n* short\n* int\n* long");
                            } else if (num >= -Math.pow(2, 15) 
                                                    && num <= Math.pow(2, 15) - 1) {
                                    System.out.println("* short\n* int\n* long");
                            } else if (num >= -Math.pow(2, 31)
                                                    && num <= Math.pow(2, 31) - 1) {
                                    System.out.println("* int\n* long");
                            } else {
                                    System.out.println("* long");
                            }
                    } catch (Exception e) {
                            System.out.println(scn.next() + " can't be fitted anywhere.");
                    }
                    t--;
            }
    
            scn.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>=-(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.");
            }
    
        }
    }
    

    }

  • + 0 comments

    Great explanation of Java’s primitive data types! It’s always helpful to break down how each type works with different bit sizes. Funinexchange Login

  • + 0 comments
    import java.io.*;
    import java.util.*;
    import java.math.BigInteger;
    
    public class Solution {
    
        public static void main(String[] args) {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            Scanner sc = new Scanner(System.in);
    
            int q = sc.nextInt();
            BigInteger input;
    
            for (int i = 0; i < q; i++) {
                String inputString = sc.next();
                input = new BigInteger(inputString);
    
                if (BigInteger.valueOf(input.longValue()).equals(input)) {
                    System.out.println(String.format("%d can be fitted in:", input));
                    if (BigInteger.valueOf(input.byteValue()).equals(input)) {
                        System.out.println("* byte");
                    }
                    if (BigInteger.valueOf(input.shortValue()).equals(input)) {
                        System.out.println("* short");
                    }
                    if (BigInteger.valueOf(input.intValue()).equals(input)) {
                        System.out.println("* int");
                    }
                    System.out.println("* long");
                } else {
                    System.out.println(String.format("%d can't be fitted anywhere.", input));
                }
            }
            sc.close();
        }
    }