Java Datatypes

  • + 0 comments

    public class Sollution {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int inputs = scan.nextInt();
    
        scan.nextLine();
        BigInteger longmax = BigInteger.valueOf(Long.MAX_VALUE);
        BigInteger longmin = BigInteger.valueOf(Long.MIN_VALUE);
        BigInteger intmax=  BigInteger.valueOf(Integer.MAX_VALUE);
        BigInteger intmin = BigInteger.valueOf(Integer.MIN_VALUE);
        BigInteger shortmin=  BigInteger.valueOf(Short.MIN_VALUE);
        BigInteger shortmax=  BigInteger.valueOf(Short.MAX_VALUE);
        BigInteger bytemin=  BigInteger.valueOf(Byte.MIN_VALUE);
        BigInteger bytemax=  BigInteger.valueOf(Byte.MAX_VALUE);
    
        for(int i=0;i<inputs;i++){
            String numtostr = scan.nextLine();
            boolean fits= false;
            try{
                BigInteger bignum = new BigInteger(numtostr);
                if(bignum.compareTo(bytemax)<=0 && bignum.compareTo(bytemin)>=0){
                    if(!fits) 
                        System.out.println(numtostr+" can be fitted in: ");
    
                    System.out.println(" byte");
                    fits= true;
                }
                if(bignum.compareTo(shortmax)<=0 && bignum.compareTo(shortmin)>=0){
                    if(!fits) 
                        System.out.println(numtostr+" can be fitted in: ");
    
                    System.out.println(" short"); 
                    fits = true;
                }
                if(bignum.compareTo(intmax)<=0 && bignum.compareTo(intmin)>=0){
                    if(!fits) 
                        System.out.println(numtostr+" can be fitted in: ");
    
                    System.out.println(" integer");
                    fits = true;
                }
                if(bignum.compareTo(longmax)<=0 && bignum.compareTo(longmin)>=0){
                    if(!fits)
                        System.out.println(numtostr+" can be fitted in: ");
    
                    System.out.println(" long");
                    fits= true;
                }
            }
            catch(Exception E){
                if(!fits)
                System.out.println(numtostr+" can't be fitted anywhere in int,short, long");    
    

    /213333333333333333333333333333333333- only this o/p is getting wrong/ } } scan.close(); } }