Java - How to parse large int as command line argument? -


I have to write a small program that works for all values ​​of n. N is a command line argument (args [0]). The problem is integer. Percierant does not work for large values ​​such as 20000000000. How can I get this problem? The program is designed to print all the values ​​that have the power of 2, as long as the value> = n and n must be argued [0].

  public class PowerOfTwo {public static zero main string [] args) {int k = 1; Int n = integer .Portset (args [0]); If (n & lt; 0) {System.out.println (""); } Else {for (int i = 1; k & lt; = n; i ++) {k * = 2; If (k & lt; = n) {System.out.println (k); } Else {System.out.println (); }}}}  

}

using java.math Please. BigInteger or java.math.BigDecimal can handle any number of magnitudes.

Your loop looks after:

  public static zero main (string [] args) {last bigenter TWO = new bigigner ("2"); BigInteger k = BigInteger.ONE; BigInteger N = New Bigtegger (Args [0]); If (n.compareTo (BigInteger.ZERO) & lt; 0) {System.out.println ("& lt; 0"); } And {while (k.compareTo (n) & lt; = 0) {k = k.multiply (TWO); If (k.compareTo (n) & lt; = 0) {System.out.println (k); } Else {System.out.println (); }}}}  

Comments