How to check a range that exists outside of Long range in Java?

So, I want to check a number that is outside the range of the Long Datatype in Java. I wrote the code, but not beyond Long. What if I gave the number more than the Long range, and it is assumed that it will be printed, that it is out of range, or something else. 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 Eran I slightly modified the code 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]); } } } 
+5
source share
3 answers

Long.parseLong throws a NumberFormatException if you pass it a number that cannot be parsed for so long. You can catch this exception and try to parse it as BigInteger . If you succeed, you will realize that input is a valid number that just doesn't fit for so long.

For instance:

  try { System.out.println("Valid Long: " + Long.parseLong("1234567890123456789012345")); } catch (NumberFormatException n) { System.out.println("Valid Big Integer: " + new BigInteger ("1234567890123456789012345").toString()); } 

Fingerprints:

 Valid Big Integer: 1234567890123456789012345 

which means that 1234567890123456789012345 is not valid for long.

If the BigInteger constructor also throws a NumberFormatException , you know that the input cannot be parsed as a valid number (this will happen, for example, if the String parsing contains non-numeric characters).

+9
source

when the number exceeds Long.MAX_VALUE, Long.parseLong throws a NumberFormatException exception you can assign a variable as BigInteger and compare with Long.MAX_VALUE.

 public void check(String data){ data = "1234567890123456789012345"; BigInteger bg = new BigInteger (data); try{ //check number is in long.MAX_VALUE System.out.println(bg.compareTo(new BigInteger (String.valueOf(Long.MAX_VALUE)))); }catch (NumberFormatException n) { System.out.println("not a valid number"); }} 
+1
source

Use BigInteger and compare it to Long.MAX_VALUE. In practice, BigInteger has no limitations.

0
source

All Articles