So I want to check the number that is beyond the range of Long Datatype in Java. I have written some code but it does not go beyond the range of Long . What if I have given the number more than range of Long and it is supposed to be print that it is beyond long range or something. here is my code :
import java.math.BigInteger;
import java.util.Scanner;
public class Solution {
public void check(String data) {
System.out.println(data + " can be fitted in:");
Long bInt = Long.parseLong(data);
if (bInt >= Byte.MIN_VALUE && bInt <= Byte.MAX_VALUE) {
System.out.println("* byte");
}
if (bInt >= Short.MIN_VALUE && bInt <= Short.MAX_VALUE) {
System.out.println("* short ");
}
if (bInt >= Integer.MIN_VALUE && bInt <= Integer.MAX_VALUE) {
System.out.println("* int ");
}
if (bInt >= Long.MIN_VALUE && bInt <= Long.MAX_VALUE) {
System.out.println("* long ");
}
}
public static void main(String args[]) {
Solution solution = new Solution();
Scanner sc = new Scanner(System.in);
int data = Integer.parseInt(sc.nextLine());
String[] array = new String[data];
Scanner sc1 = new Scanner(System.in);
for (int i = 0; i < data; i++) {
array[i] = sc1.nextLine();
}
for (int j = 0; j < array.length; j++) {
solution.check(array[j]);
}
}
}
with the help of Eran
i changed the code slightly and it works.
Updated code :
import java.math.BigInteger;
import java.util.Scanner;
/**
*
* @author pez
*/
public class Solution {
public void check(String data) {
try {
Long bInt = Long.parseLong(data);
System.out.println(data + " can be fitted in:");
if (bInt >= Byte.MIN_VALUE && bInt <= Byte.MAX_VALUE) {
System.out.println("* byte");
}
if (bInt >= Short.MIN_VALUE && bInt <= Short.MAX_VALUE) {
System.out.println("* short ");
}
if (bInt >= Integer.MIN_VALUE && bInt <= Integer.MAX_VALUE) {
System.out.println("* int ");
}
if (bInt >= Long.MIN_VALUE && bInt <= Long.MAX_VALUE) {
System.out.println("* long ");
}
} catch (NumberFormatException e) {
System.out.println(data + " can't be fitted anywhere beyond long ");
}
}
public static void main(String args[]) {
Solution solution = new Solution();
Scanner sc = new Scanner(System.in);
int data = Integer.parseInt(sc.nextLine());
String[] array = new String[data];
Scanner sc1 = new Scanner(System.in);
for (int i = 0; i < data; i++) {
array[i] = sc1.nextLine();
}
for (int j = 0; j < array.length; j++) {
solution.check(array[j]);
}
}
}