I’m really new to Java so please excuse if this isn’t the 100% right way to even write this code.
So I’ve been messing around with this code for about 6 hours now, and I can not for the life of me figure out how to fix this issue:
I have a compound interest calculator that takes user input for the variables of term length, initial amount, and APR. I can get the answer I want if it was just the simple one time calculation, but I really want it to show me the amount increased each year of a term. For example: If the interest is calculated for 10 years, I want it to show me the amount for each year leading up to it. Right now all I get is a static number of 1, or infinity.
How do I get this program to show me the total amount for the term (i.e. the length of the user input investment), broken down per year with the amount shown per year?
The code is:
import java.util.Scanner; import java.lang.Math; public class CompoundInterestCalculator { public static void main(String args[]) { Scanner scan = new Scanner(System.in); double initial; // the intial amount your loan is set for int term; // the number of years you want to calculate float yearlyrate; // interest rate int repeat; // System message to alert users to program use System.out.printf("Hi there! I can calculate compound interest for you at a fixed interest rate and term.nnPlease enter numbers without commas or symbols to get an accurate result!"); // Prompt for initial investment amount System.out.println("nPlease enter your initial investment."); // Store value of user input for initial investment initial = scan.nextDouble(); // Prompt for interest percentage System.out.println(); System.out.println("Please enter the annual interest percentage."); System.out.println(); // Store value of user input for interest amount yearlyrate = scan.nextFloat(); // Prompt for length of the term for investment System.out.println(); System.out.println("Please enter the length of your investment in years."); // Store Value of user input for term length term = scan.nextInt(); //For loop to set up calulations, repeats, and totals for(repeat = 0; repeat < term; repeat++) { System.out.println(); System.out.println("Your investment amount after" + (repeat+1) + "years is:"); // calculation for determining compound interest at a yearly rate and over the term double total = Math.pow(initial * (1 + (yearlyrate/100) / 12) , 12/term); // Displays the total value to the user System.out.println("$" + total); // Seperation line to clean it up System.out.println(); // Close the scanner scan.close(); } } }
Any help would be greatly appreciated because I am really out of my depth with this one.
Advertisement
Answer
Just a small change in your calculation logic:
// calculation for determining compound interest at a yearly rate and over the term double total = initial * Math.pow((1 + (yearlyrate/100)) , (repeat+1));