-1

So this is the code I have written: The bold doesn’t print out what I want to print out, which is if a long data type is entered then the phrase “can be fitted in a long”.

I tried using the Math.pow, but the function doesn’t works since this is for a double data type. Any advice!

import java.util.*;
import http://java.io.*;

public class HackerRank {
    public static void main(String[] args){
        Scanner in = new Scanner(System Resources and Information.);
        int n = in.nextInt();
        for(int t = 0; t < n; t++){
            try {
                long a = in.nextInt();
                if(a > -128 && a <= 127){
                    System.out.println(a + " can be fitted in:\n*byte");
                    System.out.println("*short");
                    System.out.println("*int");
                    System.out.println("*long");
                } else if(a > -32768 && a <= 32767) {
                    System.out.println(a + " can be fitted in:\n*short");
                    System.out.println("*int");
                    System.out.println("*long");
                } else if(a > Math.pow(-2, 31) && a < Math.pow(2, 31)) {
                    System.out.println(a + " can be fitted in:\n*int");
                    System.out.println("*long");
                } else {
                    if (a >= -Math.pow(2, 63) && a < Math.pow(2, 63)-1); 
                    System.out.println(a + " can be fitted in:\n*long");
                }
            } catch (Exception e){
                System.out.println(in.next() + " can't be fitted anywhere.");
            }
        }
    }
}
clemens
  • 16,716
  • 11
  • 50
  • 65
uxtrejo
  • 1
  • 1
  • you have an extra ";" at the end of the line "if (a >= -Math.pow(2, 63) && a < Math.pow(2, 63)-1); ". That will be treated as a normal statement and the scope of the if clause ended in that line itself. – sSaroj Nov 22 '17 at 05:51

2 Answers2

0

Use double instead of long because double type variables can't be stored in long.

double a = in.nextDouble();

Anurag Sharma
  • 76
  • 1
  • 5
0

Instead of calculating that result yourself, try using the follwing constants:

Integer.MIN_VALUE
Integer.MAX_VALUE
Long.MIN_VALUE
Long.MAX_VALUE
Lino
  • 19,604
  • 6
  • 47
  • 65