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



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

i need write short programme works values of n. n command line argument(args[0]). problem integer.parseint doesn't work big values such 20000000000. around problem? programme designed print values powerfulness of 2 until value >= n , n has argument[0].

public class poweroftwo{ public static void main(string[] args){ int k = 1; int n = integer.parseint(args[0]); if(n < 0){ system.out.println(""); }else{ for(int i=1; k <= n; i++){ k *= 2; if(k <= n){ system.out.println(k); }else{ system.out.println(); } } } }

}

use java.math.biginteger or java.math.bigdecimal. these can handle numbers of magnitude.

your loop like:

public static void main(string[] args) { final biginteger 2 = new biginteger("2"); biginteger k = biginteger.one; biginteger n = new biginteger(args[0]); if (n.compareto(biginteger.zero) < 0) { system.out.println("< 0"); } else { while (k.compareto(n) <= 0) { k = k.multiply(two); if (k.compareto(n) <= 0) { system.out.println(k); } else { system.out.println(); } } } }

java int

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -