Java Datatypes

Sort by

recency

|

1529 Discussions

|

  • + 0 comments

    The simplest way is

    import java.util.*;

    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");
                    System.out.println("* short");
                    System.out.println("* int");
                    System.out.println("* long");
                } else if (x >= -32768 && x <= 32767) {
                    System.out.println("* short");
                    System.out.println("* int");
                    System.out.println("* long");
                } else if (x >= -2147483648L && x <= 2147483647L) {
                    System.out.println("* int");
                    System.out.println("* long");
                } else if (x >= -9223372036854775808L && x <= 9223372036854775807L) {
                    System.out.println("* long");
                }
    
            } catch (Exception e) {
                System.out.println(sc.next() + " can't be fitted anywhere.");
            }
        }
        sc.close();
    }
    

    }

  • + 0 comments

    Working code


    import java.io.*;
    import java.util.*;
    class Solution {
     public static void main(String[] args) {
      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) {
        System.out.println(sc.next() + " can't be fitted anywhere.");
       }
    
      }
      sc.close();
     }
    }
    }
    
  • + 0 comments

    whats my mistake

  • + 1 comment

    Can someone tell me why I can't pass the private testcase

    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<=32768)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.util.Scanner;

    public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); while (t-- > 0) { String input = scanner.next(); try { long n = Long.parseLong(input); System.out.println(input + " can be fitted in:");

                if (n >= Byte.MIN_VALUE && n <= Byte.MAX_VALUE) {
                    System.out.println("* byte");
                }
                if (n >= Short.MIN_VALUE && n <= Short.MAX_VALUE) {
                    System.out.println("* short");
                }
                if (n >= Integer.MIN_VALUE && n <= Integer.MAX_VALUE) {
                    System.out.println("* int");
                }
                if (n >= Long.MIN_VALUE && n <= Long.MAX_VALUE) {
                    System.out.println("* long");
                }
            } catch (Exception e) {
                System.out.println(input + " can't be fitted anywhere.");
            }
        }
        scanner.close(); 
    }
    

    }