Java Datatypes

Sort by

recency

|

1596 Discussions

|

  • + 0 comments

    This is a great exercise for understanding Java’s primitive data types and their ranges. Ekbet

  • + 0 comments

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

    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

    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");
                }
            }
        }
    }
    

    }