Java Datatypes

Sort by

recency

|

1514 Discussions

|

  • + 0 comments

    import java.io.; import java.util.;

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

    }

  • + 0 comments

    This is the solution for all the testcases

    import java.io.*;
    import java.util.*;
    import java.math.*;
    
    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 count = Integer.parseInt(sc.nextLine());
            
            for(int i = 0; i < count;i++){
                String strNum = sc.nextLine();
                BigInteger bigNum = new BigInteger(strNum);
               
                
                if ((bigNum.compareTo(BigInteger.ZERO) > 0  && bigNum.compareTo(BigInteger.valueOf( Long.MAX_VALUE )) > 0) || (bigNum.compareTo(BigInteger.ZERO) < 0  && bigNum.compareTo(BigInteger.valueOf( Long.MIN_VALUE )) < 0)){
                    System.out.println(bigNum + " can't be fitted anywhere.");
                } else
                    System.out.println( bigNum + " can be fitted in:");
                
                if (bigNum.compareTo(BigInteger.valueOf(Byte.MAX_VALUE )) <= 0 && bigNum.compareTo(BigInteger.valueOf(Byte.MIN_VALUE )) >= 0){
                    System.out.println("* byte");
                }
                if (bigNum.compareTo(BigInteger.valueOf(Short.MAX_VALUE )) <= 0 && bigNum.compareTo(BigInteger.valueOf(Short.MIN_VALUE )) >= 0){
                    System.out.println("* short");
                }
                if (bigNum.compareTo(BigInteger.valueOf(Integer.MAX_VALUE )) <= 0 && bigNum.compareTo(BigInteger.valueOf(Integer.MIN_VALUE )) >= 0){
                    System.out.println("* int");
                }
                if (bigNum.compareTo(BigInteger.valueOf(Long.MAX_VALUE )) <= 0 && bigNum.compareTo(BigInteger.valueOf(Long.MIN_VALUE )) >= 0) {
                    System.out.println("* long");
                }
            }
        }
    }
    
  • + 1 comment

    Why is this wrong, when it gives the exact output as the expected?

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

    guys testcases 2/4 is passed remaing two is failed give me the solution

  • + 1 comment

    The sample solution has you looking up the values of the min and max numbers. Much better to use the static fields

            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.");
                }
    
            }