Java Datatypes

Sort by

recency

|

1594 Discussions

|

  • + 0 comments

    My code is not passing the 2nd and 3rd Testcases.... how do i clear them?!

  • + 0 comments
    public class Solution {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int myInt = scanner.nextInt();
            long num;
            
            for(int i = 1; i <= myInt; i++){
                try{
                    num = scanner.nextLong();
                    System.out.println(num + " can be fitted in:");
                    
                    if(num >= Byte.MIN_VALUE && num <= Byte.MAX_VALUE){
                        System.out.println("* byte");
                        System.out.println("* short");
                        System.out.println("* int");
                        System.out.println("* long");
                    } else if(num >= Short.MIN_VALUE && num <= Short.MAX_VALUE){
                        System.out.println("* short");
                        System.out.println("* int");
                        System.out.println("* long");
                    } else if(num >= Integer.MIN_VALUE && num <= Integer.MAX_VALUE){
                        System.out.println("* int");
                        System.out.println("* long");
                    } else {
                        System.out.println("* long");
                    }
                    
                } catch (Exception e){
                    System.out.println(scanner.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);
        String s = sc.next();
        while (sc.hasNext()) {
            s = sc.next();
            boolean fitted = false, bFit = false, shFit = false, iFit = false, lFit = false;
            try {
                byte b = Byte.valueOf(s);
                if (b >= Byte.MIN_VALUE && b <= Byte.MAX_VALUE) {
                    bFit = true;
                    fitted = true;
                }
            } catch (Exception e) {
            }
    
            try {
                short sh = Short.valueOf(s);
                if (sh >= Short.MIN_VALUE && sh <= Short.MAX_VALUE) {
                    shFit = true;
                    fitted = true;
                }
            } catch (Exception e) {
            }
    
            try {
                int i = Integer.valueOf(s);
                if (i >= Integer.MIN_VALUE && i <= Integer.MAX_VALUE) {
                    iFit = true;
                    fitted = true;
                }
            } catch (Exception e) {
            }
            try {
                long l = Long.valueOf(s);
                if (l >= Long.MIN_VALUE && l <= Long.MAX_VALUE) {
                    lFit = true;
                    fitted = true;
                }
            } catch (Exception e) {
            }
            if (!fitted) {
                System.out.println("".concat(s.concat(" can't be fitted anywhere.")));
            } else {
                System.out.println(s.concat(" can be fitted in:"));
                if (bFit) {
                    System.out.println("* byte");
                }
                if (shFit) {
                    System.out.println("* short");
                }
                if (iFit) {
                    System.out.println("* int");
                }
                if (lFit) {
                    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>=-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>=-(Math.pow(2, 31)) && x<=(Math.pow(2, 31))-1)System.out.println("* int\n* long");
                else 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");
                if (x >= -9223372036854775808L && x <= 9223372036854775807L)
                System.out.println("* long");   
                }
    
            catch(Exception e)
            {
                System.out.println(sc.next()+" can't be fitted anywhere.");
            }
    
        }
    }
    

    }