Java Datatypes

Sort by

recency

|

1565 Discussions

|

  • + 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 NumberOfInput= sc.nextInt();
    
    
       for (int i = 0; i < NumberOfInput; i++) {
    
    
    
            try{
                Long InputValue= sc.nextLong();
                 System.out.println(InputValue+" "+"can be fitted in:");
             if (InputValue>=Byte.MIN_VALUE &&InputValue<=Byte.MAX_VALUE) {
                 System.out.println("* byte");
             }
             if (InputValue>=Short.MIN_VALUE &&InputValue<=Short.MAX_VALUE) {
                 System.out.println("* short");
             }
              if (InputValue>=Integer.MIN_VALUE &&InputValue<=Integer.MAX_VALUE) {
                 System.out.println("* int");
             }
    
               if (InputValue>=Long.MIN_VALUE && InputValue<=Long.MAX_VALUE) {
                 System.out.println("* long");
             }
    
    
            }catch(Exception e){
                System.out.println(sc.next() +" "+"can't be fitted anywhere.");
            }
    
    
    
    
    
       }
    }
    

    }

  • + 0 comments

    Here is my sol

    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. */
            String na = "%s can't be fitted anywhere.";
            Scanner sc = new Scanner(System.in);
            sc.nextLine();
            while(sc.hasNextLine()){
                String num = sc.nextLine();
                Boolean previouslyCalled = false;
                Object[] obj = new Object[]{Byte.class, Short.class, Integer.class, Long.class};
                for(int type=0; type<4; type++){
                    if(obj[type] == Byte.class){
                        try{
                            Byte byteNum = Byte.parseByte(num);
                            if(!previouslyCalled){
                                System.out.printf("%d can be fitted in:%n", byteNum);
                                previouslyCalled = true;
                            }
                            System.out.println("* byte");
                        }
                        catch(NumberFormatException ex){
                        }
                    }
                    else if(obj[type] == Short.class){
                        try{
                            Short shortnum = Short.parseShort(num);
                            if(!previouslyCalled){
                                System.out.printf("%d can be fitted in:%n", shortnum);
                                previouslyCalled = true;
                            }
                            System.out.println("* short");
                        }
                        catch(NumberFormatException ex){
                        }
                    }
                    else if(obj[type] == Integer.class){
                        try{
                            int intnum = Integer.parseInt(num);
                            if(!previouslyCalled){
                                System.out.printf("%d can be fitted in:%n", intnum);
                                previouslyCalled = true;                           
                            }
                            System.out.println("* int");
                        }
                        catch(NumberFormatException ex){
                        }
                    }
                    else if(obj[type] == Long.class){
                        try{
                            long longnum = Long.parseLong(num);
                            if(!previouslyCalled){
                                System.out.printf("%d can be fitted in:%n", longnum);
                                previouslyCalled = true;
                            }
                            System.out.println("* long");
                        }
                        catch(NumberFormatException ex){
                        }
                    }
                }
                if (!previouslyCalled){
                    System.out.println(String.format(na, num));
                }
            }
            sc.close();
        }
    }
    
  • + 0 comments

    import java.util.Scanner; public class DataTypeFitting { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt();

        for (int i = 0; i < t; i++) {
            try {
                long n = scanner.nextLong(); 
    
                System.out.println(n + " can be fitted in:");
                if (n >= -128 && n <= 127) System.out.println("* byte");
                if (n >= -32768 && n <= 32767) System.out.println("* short");
                if (n >= -2147483648L && n <= 2147483647L) System.out.println("* int");
                if (n >= -9223372036854775808L && n <= 9223372036854775807L) System.out.println("* long");
    
            } catch (Exception e) {
                System.out.println(scanner.next() + " can't be fitted anywhere.");
            }
        }
    }
    

    }

  • + 1 comment

    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");}                
                //Complete the code
            }
            catch(Exception e)
            {
                System.out.println(sc.next()+" can't be fitted anywhere.");
    
    
            }
    
        }
    }
    

    }

    • + 0 comments

      is this a wrong solution

  • + 0 comments
    try
                {
                    long x=sc.nextLong();
                    System.out.println(x+" can be fitted in:");
                    if(x>=-128 && x<=127)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");
                    //input allway fit to long
                    System.out.println("* long");
                }
                catch(Exception e)
                {
                    System.out.println(sc.next()+" can't be fitted anywhere.");
                }