Java Datatypes

Sort by

recency

|

1603 Discussions

|

  • + 0 comments

    It's not the best example for a solution, I'm just posting my solution:

    import java.io.; import java.math.BigInteger; import java.util.;

    public class DatatypeFitter {

    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    
        int q = Integer.parseInt(bufferedReader.readLine().trim());
    
        for (int i = 0; i < q; i++) {
            String strNum = bufferedReader.readLine().trim();
    
            BigInteger numBigDecimal = new BigInteger(strNum);
    
            int isBiggerThanLong = numBigDecimal.compareTo(BigInteger.valueOf(Long.MAX_VALUE));
            int isSmallerThanLong = numBigDecimal.compareTo(BigInteger.valueOf(Long.MIN_VALUE));
    
            if (isBiggerThanLong > 0 || isSmallerThanLong < 0) {
                System.out.println(strNum + " can't be fitted anywhere.");
            } else {
                System.out.println(strNum + " can be fitted in:");
    
                long num = numBigDecimal.longValue();
    
    
                if (num >= Byte.MIN_VALUE && num <= Byte.MAX_VALUE) {
                    System.out.println("* byte");
                }
                if (num >= Short.MIN_VALUE && num <= Short.MAX_VALUE) {
                    System.out.println("* short");
                }
                if (num >= Integer.MIN_VALUE && num <= Integer.MAX_VALUE) {
                    System.out.println("* int");
                }
                System.out.println("* long");
            }
        }
    }
    

    }

  • + 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 >= 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) {
                // Consume the input that caused the exception
                String badInput = sc.next();
                System.out.println(badInput + " can't be fitted anywhere.");
            }
        }
    }
    

    }

  • + 0 comments

    If you’re looking to grow your online presence and attract more customers, choosing the right SEO company in Multan can make all the difference

  • + 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 >= -(Math.pow(2,7)) && x <= Math.pow(2,7) - 1 ){
                        System.out.println("* byte");
                    }
                    if(x >= -(Math.pow(2,15)) && x <= Math.pow(2,15) - 1){
                        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
    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");
                    }
                
                    System.out.println("* long");
         
                    //Complete the code
                }
                catch(Exception e)
                {
                    System.out.println(sc.next()+" can't be fitted anywhere.");
                }
    
            }
        }
    }