Java Datatypes

  • + 0 comments

    Idk, i dont manage to think as the others.

    is it so wrong, solving the problem like this ?

    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();
                    String s = String.valueOf(x);
                    System.out.println(x+" can be fitted in:");
                    if(checkByte(s)){
                        System.out.println("* byte");
                    }
                    if(checkShort(s)){
                        System.out.println("* short");
                    }
                    if(checkInt(s)){
                        System.out.println("* int");
                    }
                      if(checkLong(s)){
                        System.out.println("* long");
                    }
                    
                }
                catch(Exception e)
                {
                    System.out.println(sc.next()+" can't be fitted anywhere.");
                }
    
            }
            
        }
        private static boolean checkByte(String l){
                try{
                    byte b =Byte.valueOf(l);
                    return true;
                }catch(Exception e){
                    return false;
                }
            }
             private static boolean checkShort(String l){
                  try{
                    short s = Short.valueOf(l) ;
                    return true;
                }catch(Exception e){
                    return false;
                }
            
            }
             private static boolean checkInt(String l){
             try{
                int i = Integer.valueOf(l);
                    return true;
                }catch(Exception e){
                    return false;
                }
            }
            private static boolean checkLong(String l){
                try {
                    long s = Long.valueOf(l);
                    return true;
                } catch (Exception e) {
                    return false;
                }
            }
            
    }