I am a beginner at coding Java and am having a tough time understanding what is wrong with this code. This is an assignment I had posted about earlier and I also went to a Java tutor at my school for help after having issues communicating with my actual teacher (I am an online student). She gave me 10%. I will include instructions and the criteria she offered when she said I could "r&r", revise & resubmit. She seems to want something extremely specific and I'm not exactly sure what she wants.


Create a Java file called CompoundInterestYourLastName. Write a method called computeBalance( ) that computes the balance of a bank account with a given initial balance and interest rate, after a given number of years. Assume interest is compounded yearly.

You can use the standard calculator here to check your results (note that you'll have to change the dropdown to compound the interest yearly): http://www.thecalculatorsite.com/finance/calculators/compoundinterestcalculator.php

Use a loop to control the iterations through the years in your method.

Your method should return a double value.

In your main method, run the following tests to verify your method is working correctly.

System.out.printf("Your total is $%.2f", computeBalance(1000, .045, 3));
// should return $1141.17
System.out.printf("\nYour total is $%.2f", computeBalance(2000, .03, 5));
// should return $2318.55
System.out.printf("\nYour total is $%.2f", computeBalance(3000, .01, 10));
// should return $3313.87

Pay close attention to your parameters to make sure they match the test datat!

*Note: Your methods should have this exact signature line, including the correct spelling, capitalization and parameter types. Otherwise, when I run the test to check the file, your methods will fail and you'll have to revise.

When you are finished with your file, upload it to Blackboard. This program is worth 10 points.

CRITERIA SHE OFFERED: Use a simple formula in your loop to return the results outlined in the assignment. You don't need to use Math.pow to get this to work. r&r

 // Tori Tidwell
 import java.util.Scanner;
 import java.lang.Math;
 public class CompoundInterestTidwell {

 public static void main(String[] args) {
     Scanner sc = new Scanner(System.in);

    System.out.print("   Inital Balance Amount:     ");
    double P = sc.nextDouble();
    System.out.print("   Interest rate:     ");
    double r = sc.nextDouble();
    System.out.print("   Number of years in account:     ");
    int t = sc.nextInt();
    double compInt = computeBalance(P, r, t);

    System.out.printf("   Your new balance is $%.2f", compInt);

public static double computeBalance(double P, double r, int t) {
    // Formula for compounding interest
    // A = P(1+(r/n))^(n(t))
    // Java tutor in AAC told me to use c instead of n*t. Math worked after this.

    double compInt = 0;
    for(int c = 0; c <= t; c++ ) {

        compInt = P*Math.pow((1+r/100), c);

    return compInt;


closed as off-topic by Stephen Rauch, AJNeufeld, Mast, t3chb0t, Vogel612 Oct 23 '18 at 10:22

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – AJNeufeld, Mast, t3chb0t, Vogel612
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    \$\begingroup\$ Within your loop, you overwrite compInt, so only the last loop iteration is meaningful. Either remove the loop, or use a simple multiplication inside the loop, \$\endgroup\$ – Cris Luengo Oct 21 '18 at 18:58
  • \$\begingroup\$ What kind of multiplication? Using a carrot sign like in math doesn't work. Searching the internet gives me Math.pow again. I need to know more descriptively what else I should be doing, thanks. \$\endgroup\$ – Tori Oct 21 '18 at 20:07
  • 1
    \$\begingroup\$ For example, a 4% annual interest rate means multiplying by 1.04 (104%) every year. You do this in a loop t times, with t the number of years. This is equivalent to multiplying once by pow(1.04,t). \$\endgroup\$ – Cris Luengo Oct 21 '18 at 20:12

First, to fix your formula:

r probably shouldn't be divided by 100, since the argument is .045 in one of the examples, which I verified to mean 4.5% rather than 0.045%.

Now, the loop logic:

Your current code replaces compInt each time the for loop runs, so it does something like this.

Example: P=1000, r=0.045, t=3

compInt = 0
compInt = 1000 * 1.045 ^ 0
compInt = 1000 * 1.045 ^ 1
compInt = 1000 * 1.045 ^ 2
compInt = 1000 * 1.045 ^ 3

There is no need to perform any of the calculations except for the last line.

However, since your teacher wants you to not use Math.pow and use a loop. So think: what gets repeated when you do Math.pow? Multiplication, of course. So you simply multiply the principal by 1+r for each year.

Since it's homework, I'm not going to post the revised code, but I hope I've given you an idea of where to go.


Not the answer you're looking for? Browse other questions tagged or ask your own question.