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