Java Hex Number and Conditionals

2

I am new to Java so I have a problem with hex numbers usage in conditionals and its sizes. There is some issue about Scanner class too. I have searched java documents for primitive data types.

The program is about; take a number and look in which data type it could be written and print the appropriate data type.

import java.util.*;
import java.io.*;
import java.lang.Integer;


public class Solution{
   public static void main(String []argh)
    {
      Scanner sc = new Scanner(System.in);
      long 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>=0x81 || x<=0x7f)System.out.println("* byte");
            if(x<=0x7fff || x>=0x8001)System.out.println("* short");
            if(x<=0x7fffffff || x>=0x80000001)System.out.println("* int");

            //if(x<= (0x7fffffffffffffff)|| x>= 
                       //(0x8000000000000001))System.out.println("*long");
            System.out.printf("\n\n%x\n\n",x);
            System.out.println();
        }
            catch(Exception e)
        {
            System.out.println(sc.next()+" can't be fitted anywhere.");
        }

      }
   }
 }

Questions:

  1. I get an error message for commented "if()" conditional "too large int :0x7fffffffffffffff " why is that so?

  2. I run the program by commenting some "if()" and give it "10" to search possible data types but this time it prints just only "int" datatype.

  3. If I give it "0x7b" as input it carries out to "catch()" section. Why?

Could you please explain it ? Thanks.

java
types
java.util.scanner
asked on Stack Overflow Sep 15, 2018 by BTech

2 Answers

1

I get an error message for commented "if()" conditional "too large int :0x7fffffffffffffff " why is that so?

Because 0x7fffffffffffffff is an int literal. 7fffffffffffffff can't fit into an int. It can fit into a long though. To make it a long literal, you have to add l or L at the end:

if(x<= (0x7fffffffffffffffL)|| x>=
    (0x8000000000000001L))System.out.println("*long");

But this if statement is redundant, because x is already of type long, so by definition, its value must fit in long.

Actually, using the MAX_VALUE and MIN_VALUE constants of each type is less error-prone to write than numeric literals:

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");
System.out.println("*long");

I run the program by commenting some "if()" and give it "10" to search possible data types but this time it prints just only "int" datatype.

I cannot reproduce this. Which if statements did you comment out?

If I give it "0x7b" as input it carries out to "catch()" section. Why?

Scanner.nextLong does not recognise hex numbers, unfortunately. You can read the number as a string, check its prefix, if it's 0x, then strip off the first two characters, and parse it as a hex number.

answered on Stack Overflow Sep 15, 2018 by Sweeper • edited Sep 15, 2018 by Sweeper
0

1) this number does not fit into an integer, use a long literal: 0x7fffffffffffffffL

2) unclear, which if did you comment out?

3) this format is not understood by the Scanner, use decimal representation

But, you should also reconsider your conditions. They don't work the way you intended. For example 300 would be classified as byte. (because 300 > 129).

answered on Stack Overflow Sep 15, 2018 by Henry

User contributions licensed under CC BY-SA 3.0