We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Java
  3. Introduction
  4. Java Datatypes
  5. Discussions

Java Datatypes

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 1369 Discussions, By:

recency

Please Login in order to post a comment

  • _jimmy_
    3 days ago+ 2 comments

    JAVA 8 - An easy way.

    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) {
                    System.out.println(sc.next() + " can't be fitted anywhere.");
                }
            }
            
            sc.close();
        }
    }
    
    1|
    Permalink
  • cerose_1991
    1 week ago+ 0 comments

    JAVA 15

    Scanner scan = new Scanner(System.in); long n = scan.nextLong();

    for (long i = 0L; i <= n ; i ++){
    
        while(scan.hasNext()){
    
        try{
    
            long number = scan.nextLong();
    
            System.out.println(number+" can be fitted in:" );
    
            if ((number >= -128) && (number <= 127)){
                System.out.println("* byte");
            }
    
            if ((number >= -32768) && (number <= 32767)){
                System.out.println("* short");
            }
    
            if ((number >= -Math.pow(2,31)) && (number <= (Math.pow(2,31) - 1))) {
                System.out.println("* int");
            }
    
            if ((number >= -Math.pow(2,63)) && (number <= (Math.pow(2,63) - 1))) {
                System.out.println("* long");
            }
    
        }
        catch(Exception e){
            System.out.println(scan.next() + " can't be fitted anywhere.");
        }
    
      }
    }
    
    0|
    Permalink
  • salehsami017
    2 weeks ago+ 1 comment

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

    public class Solution {

    public static void main(String[] args) {
    
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
    
        for (int i = 0; i <= n - 1; i ++){
    
            try{
    
                long number = input.nextLong();
    
                System.out.println(String.format("%d can be fitted in:" ,number));
    
                if ((number >= -128) && (number <= 127)){
                    System.out.println("* byte");
                }
    
                if ((number >= -32768) && (number <= 32767)){
                    System.out.println("* short");
                }
    
                if ((number >= -Math.pow(2,31)) && (number <= (Math.pow(2,31) - 1))) {
                    System.out.println("* int");
                }
    
                if ((number >= -Math.pow(2,63)) && (number <= (Math.pow(2,63) - 1))) {
                    System.out.println("* long");
                }
    
            }
            catch(Exception e){
                System.out.println(input.next() + " can't be fitted anywhere.");
            }
        }
    }
    

    }

    0|
    Permalink
  • lukkonina1
    2 weeks ago+ 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|
    Permalink
  • umutyildirim1905
    2 weeks ago+ 0 comments
    import java.io.*;
    import java.util.*;
    
    public class DataTypesExample {
    
    
        public static void main(String[] args) {
            long a;
            Scanner sc = new Scanner(System.in);
    
            int numOfQueries = Integer.parseInt(sc.nextLine());
            
            long[] nums = new long[numOfQueries];
            for (long k : nums)
            {
                try {
                    nums[(int) k] = sc.nextLong();
                    a = nums[(int) k];
                    if(a>=-128 && a<=127)//*** byte range is -128 to 127
                    {
                        System.out.println(a +" can be fitted in: ");
                        System.out.println("* byte\n* short\n* int\n* long");
                    }
                    else if(a>=-32768  && a<=32767 )//*** short range is -32768 to 32767
                    {
                        System.out.println(a +" can be fitted in: ");
                        System.out.println("* short\n* int\n* long");
                    }
                    else if(a>=-2147483648  && a<=2147483647 )//*** int range is -2147483648 to 2147483647
                    {
                        System.out.println(a +" can be fitted in: ");
                        System.out.println("* int\n* long");
                    }
                    else if(a>=-9223372036854775808L   && a<=9223372036854775807L)//*** long range is -9223372036854775808
                    // to 9223372036854775807
                    {
                        System.out.println(a +" can be fitted in: ");
                        System.out.println("* long");
                    }
                    else {
                        throw new Exception("test");
                    }
                }
    
                catch (Exception e)
                {
                    System.out.println(sc.nextLine() + " can't be fitted anywhere.");
                }
    
            }
    
    
        }
    }
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy