This week's book giveaways are in the Java EE and JavaScript forums. We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line! See this thread and this one for details.

using BigInteger instead of int, when I run the code below it compiles but I get an error on line 35

Line 35 from the code below is,

from what I am able to discern from the java.math.BigInteger class this is how this method should be declared. Can anyone help? [CODE] import java.io.*; import java.math.BigInteger;

class ComputeSumApp { static int aNumber; static int x = 1; static BigInteger base = BigInteger.valueOf (x);

public static void main(String[] args) throws IOException { System.out.println("Enter a number: "); aNumber = getInt(); BigInteger number = BigInteger.valueOf (aNumber); BigInteger answer = computeSum(number); System.out.println("Sum of the first N postitive BigInteger = " + answer); } public static BigInteger computeSum(BigInteger n) { if (n==base) return base; else return (n.add(computeSum(n.subtract(base))) ); } public static String getString() throws IOException { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String s = br.readLine(); return s; } public static int getInt() throws IOException { String s = getString(); return Integer.parseInt(s); }

Hello Gerald, The condition n==base compares the reference of the BigInteger instances and NOT their values, thus this condition is always FALSE as base and n refers to a different instances. Try the following: