title: Java Computing Monthly Payments Loops
body: <p>I have a question for yall, I am using loops for the following program and I cant understand how to make it so it displays each payment per month,</p>
  
  <p>You are going to write a program which will display the payment schedule for a loan given an interest rate and the number of payments.  </p>
  
  <p>Prompt the user for a loan amount, the annual interest rate, number of years to pay off the loan.  First, you will calculate the monthly payment using a formula below.  Then you will write a loop displaying the monthly payment breakdown: interest amount, principal applied to the loan, and the balance of the loan.  </p>
  
  <p>For a 1 year loan (12 monthly payments) for $10,000 at 7%, the payment breakdown looks like the following:</p>
  
  <p>monthly payment = 865.27
    payment:1     interest: 58.33     principal: 806.93       balance: 9193.07</p>
  
  <p>HERE IS THE ISSUE ABOVE I CANT GET IT TO DISPLAY THE AMOUNT OF PAYMENTS FOR (x) MONTHS AND FOR IT TO DISPLAY THE REST OF THE INFO WITH IT.</p>
  
  <pre><code>import java.text.NumberFormat;
  import java.util.Scanner;
  
  public class MothlyPay {
  
     public static double calculateMonthlyPayment(
        int loanAmount, int termInYears, double interestRate) {
  
        // Convert interest rate into a decimal
        // eg. 6.5% = 0.065
  
        interestRate /= 100.0;
  
        // Monthly interest rate 
        // is the yearly rate divided by 12
  
        double monthlyRate = interestRate / 12.0;
  
        // The length of the term in months 
        // is the number of years times 12
  
        int termInMonths = termInYears * 12;
  
        // Calculate the monthly payment
        // Typically this formula is provided so 
        // we won't go into the details
  
        // The Math.pow() method is used calculate values raised to a power
  
        double monthlyPayment = 
           (loanAmount*monthlyRate) / 
              (1-Math.pow(1+monthlyRate, -termInMonths));
  
        return monthlyPayment;
     }
  
     public static void main(String[] args) {
  
        // Scanner is a great class for getting 
        // console input from the user
  
        Scanner scanner = new Scanner(System.in);
  
        // Prompt user for details of loan
  
        System.out.print("Enter loan amount: ");
        int loanAmount = scanner.nextInt();
  
        System.out.print("Enter loan term (in years): ");
        int termInYears = scanner.nextInt();
  
        System.out.print("Enter interest rate: ");
        double interestRate = scanner.nextDouble();
  
        // Display details of loan
  
        double monthlyPayment = calculateMonthlyPayment(loanAmount, termInYears, interestRate);
        double totalPayed = 0;
        int month = 1;
        double loanAmountRemaining;
        // NumberFormat is useful for formatting numbers
        // In our case we'll use it for 
        // formatting currency and percentage values
        while(totalPayed &lt;= loanAmount){
            totalPayed = totalPayed + monthlyPayment;
            double totalLoanAmount =  loanAmount + interestRate;
            loanAmountRemaining = totalLoanAmount - totalPayed;
            month ++;
        }
        if(monthlyPayment &gt; loanAmount)
            totalPayed = totalPayed + loanAmountRemaining;
     }
  
        // Display details of the loan
   //HERE IS THE ISSUE BELOW, I CANT GET IT TO DISPLAY THE AMOUNT OF PAYMENTS FOR (x) MONTHS AND FOR IT TO DISPLAY THE REST OF THE INFO WITH IT FOR THE FOLLOWING VARIABLE LISTED IN THE PRINTF STATEMENT.
  
        System.out.printf("%9s      %9s     %9s     %9s\n", "monthlypayment", "interestRate", "loanAmount", "loanAmountRemaining");
     }
  </code></pre>
  
  <p>//When user put in the loan amount, interest rate, months, It needs display like the picture below.</p>
  
  <p><img src="https://i.stack.imgur.com/rGt2Q.jpg" alt="enter image description here"></p>