Java Datatypes

Sort by

recency

|

1602 Discussions

|

  • + 0 comments

    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 >= 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) {
                // Consume the input that caused the exception
                String badInput = sc.next();
                System.out.println(badInput + " can't be fitted anywhere.");
            }
        }
    }
    

    }

  • + 0 comments

    If you’re looking to grow your online presence and attract more customers, choosing the right SEO company in Multan can make all the difference

  • + 0 comments
    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 >= -(Math.pow(2,7)) && x <= Math.pow(2,7) - 1 ){
                        System.out.println("* byte");
                    }
                    if(x >= -(Math.pow(2,15)) && x <= Math.pow(2,15) - 1){
                        System.out.println("* short");
                    }
                    if(x >= -(Math.pow(2,31)) && x <= Math.pow(2,31) - 1){
                        System.out.println("* int");
                    }
                    if(x >= -(Math.pow(2,63)) && x <= Math.pow(2,63) - 1){
                        System.out.println("* long");
                    }
                }
                catch(Exception e)
                {
                    System.out.println(sc.next()+" can't be fitted anywhere.");
                }
    
            }
        }
    }
    
  • + 0 comments
    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<=32767){
                        System.out.println("* short");
                    }
                    if(x>=-2147483648 && x<=2147483647){
                         System.out.println("* int");
                    }
                
                    System.out.println("* long");
         
                    //Complete the code
                }
                catch(Exception e)
                {
                    System.out.println(sc.next()+" can't be fitted anywhere.");
                }
    
            }
        }
    }
    
  • + 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;
                }
            }
            
    }