0

I need to write a short program that works for all values of n. n is a command line argument(args[0]). The problem is Integer.parseInt doesn't work for large values such as 20000000000. What could i do to get around this problem? The program is designed to print all values that are the power of 2 until that value is >= n and n has to be 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();
            }
        }
    }
}

}

1
  • 2
    Try passing the string into the BigInteger(String) constructor? Commented Oct 27, 2014 at 11:34

4 Answers 4

2

Use java.math.BigInteger or java.math.BigDecimal. These can handle numbers of any magnitude.

Your loop would then look like:

public static void main(String[] args) {
    final BigInteger TWO = 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();
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot. It's seems a bit more complicated than what i have learned so far in my current course, but i think i can make sense of it.
0

What is the maximum value you are expecting to process?

  • For large numbers up to 2^63 - 1, you can use long - see more information on data types, you'll need to change Integer.parseInt() to Long.parseLong().
  • If you genuinely need arbitrarily large numbers use BigInteger. To do this you'll also need to change your variable k to be a BigInteger and use BigInteger.multiply() instead of *= and construct your variable n using new BigInteger(String value)

1 Comment

Thanks for the explanation. I have no experience using BigInteger. The assignment requires it to work for ALL values of n. So BigInteger would be the correct way to go.
0

Try following code:

public class PowerOfTwo{
public static void main(String[] args){
    long k = 1;

    long n = Long.parseLong("20000000000");
    BigDecimal bigDec=new BigDecimal(n);
    if(n < 0){
        System.out.println("");
    }else{
        for(long i=1; k <= bigDec.longValue(); i++){
            k *= 2;
            if(k <= n){
                System.out.println(k);
            }else{
                System.out.println();
            }
        }
    }
  }
}

Output :

2
4
8
16
32
64
128
256
512
1024
2048
4096
8192
16384
32768
65536
131072
262144
524288
1048576
2097152
4194304
8388608
16777216
33554432
67108864
134217728
268435456
536870912
1073741824
2147483648
4294967296
8589934592
17179869184

2 Comments

Using long just pushes the problem up to bigger numbers.
Thanks, this does it for the values of n our code evaluation program requires :)
0

In my opinion the safest and simplest way to do this is:

public class ReadBigInteger {
    public static void main(String[] args) {
        String number = args[0];
        if (number == null) {
            throw new IllegalArgumentException();       
        }

        System.out.println("The number: " + number);
        char [] tmp = number.toCharArray();

        int result = 0;
        int index = 1;

        for (int t = tmp.length - 1; t >= 0; t--) {
            int n = Integer.parseInt(tmp[t] + "");

            int toAdd = (int) (n == 0 ? (Math.pow(10, index-1)) : (n * (Math.pow(10, index-1))));

            if (toAdd > Integer.MAX_VALUE - result) {
                throw new IllegalArgumentException("Number to large");
            }
            result += toAdd;
            index++;
        }

        System.out.println("Entered integer: " + result);
    } 
}

Basically read the argument as a string. Parse it in reverse order by building the integer as you go along. An exception will be thrown if the result is greater than Integer.MAX_VALUE

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.