Java Datatypes

Sort by

recency

|

1579 Discussions

|

  • + 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();
    }
    

    }

  • + 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>=-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.");
                }
    
            }
        }
    }
    
  • + 0 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.");
         }
    
  • + 0 comments

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

    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 t = sc.nextInt();
        sc.nextLine();
        long t0 = 0;
        for (int i = 1;i<=t;i++){
            String x = sc.nextLine();
    
            try{
                t0 = Long.parseLong(x);
                if (t0>2147483647 || t0<-2147483648 ){
                    System.out.println(x+" can be fitted in:\n* long");
                } else if (t0>32767 || t0 < -32768){
                    System.out.println(x+" can be fitted in:\n* int\n* long");  
                } else if (t0>127 || t0<-128){
                    System.out.println(x+" can be fitted in:\n* short\n* int\n* long");  
                } else {
                    System.out.println(x+" can be fitted in:\n* byte\n* short\n* int\n* long");  
    
                }
            } catch (NumberFormatException e){
               System.out.println(x+" can't be fitted anywhere.") ;
            }
        }
    }
    

    }

  • + 0 comments

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

    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 scan = new Scanner(System.in);
        int t = scan.nextInt();
        boolean typeFit[] = {false,false,false,false};
        boolean fit = false;
        String types[] = {"* byte","* short","* int","* long"};
        scan.nextLine();
        for (int i=0; i<t; i++) {
            String sample = scan.nextLine();
            long sampleLong = 0l;
            try {
                System.out.print(sample);
                sampleLong = Long.parseLong(sample);
                if(sampleLong >=-128 && sampleLong <= 127) {
                    typeFit[0]=true;
                    fit=true;
                }
                if(sampleLong >=-32768 && sampleLong<= 32767) {
                    typeFit[1]=true;
                    fit=true;
                }
                if(sampleLong >=Math.pow(-2,31) && sampleLong<= Math.pow(2,31)-1) {
                    typeFit[2]=true;
                    fit=true;
                }
                if(sampleLong >=Math.pow(-2,63) && sampleLong<= Math.pow(2,63)-1) {
                    typeFit[3]=true;
                    fit=true;
                }
            } catch(Exception e) {
                System.out.println(" can't be fitted anywhere.");
    
            }
            if(fit)
               System.out.println(" can be fitted in:");
            for(int c =0; c<4;c++) {
                if(typeFit[c]) {
                    System.out.println(types[c]);
                    typeFit[c]=false;
                }
            }
            fit=false;
        }
    }
    

    }