Java Datatypes

  • + 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();
        }
    }