Home > Back-end >  Why is my output file printing the payment info backwards?
Why is my output file printing the payment info backwards?

Time:10-06

Why does my text file print the code backward? I need it to print the loan balance decreasing from top to bottom. I didn't include the program that will call the methods, let me know if I should post that as well.

Also, if there are any other discrepancies anyone might see, let me know. Thank you!

           package amortizationpack;

import java.io.*;
import java.util.Scanner;
import java.lang.Math;
import java.text.DecimalFormat;

public class Amortization {
    
    double loanAmount;
    double interestRate;
    double loanBalance;
    double term;
    double payment;
    int loanYears;
    Scanner keyboard = new Scanner(System.in);
    DecimalFormat number = new DecimalFormat("#,##0.00");

    public Amortization(double userLoanAmount, int userLoanYears, double userInterestRate) {
        
        loanAmount = userLoanAmount;
        loanYears = userLoanYears;
        interestRate = userInterestRate;

        calcPayment();
        
} // constructor
    public void calcPayment() {
        term = (Math.pow((1   interestRate / 12), (loanYears * 12)));
        payment = (loanAmount * (interestRate / 12) * term) / (term - 1);
    } // calcPayment method
    
    public int getNumberOfPayments() {
        return loanYears * 12;
    } // getNumberofPayments
    public void saveReport(String loanFile) throws IOException{
        double monthlyInterest;
        double principal;
        
        File file = new File("LoanReport.txt");
        FileWriter fileWrite = new FileWriter(file);
        PrintWriter outputFile = new PrintWriter(fileWrite);
        outputFile.println("Monthly payment: "   number.format(payment));
        outputFile.println("Month\t\t"   "Interest\t\t"   "Principal\t\t"
                  "Balance");
        outputFile.println("--------------------------------------------"
                  "-------------------");
        
    for (int m = 1; m <= getNumberOfPayments(); m  ) {
    
        monthlyInterest = interestRate / 12.0 * loanBalance;
        
        if (m != getNumberOfPayments()) {
            principal = payment - monthlyInterest;
        }
        
        else {
            principal = loanBalance;
            payment = loanBalance   monthlyInterest;
        } // for last payment
        loanBalance = loanBalance - principal;
        outputFile.print(m   "\t\t" 
          number.format(monthlyInterest)   "\t\t"    number.format(principal)   "\t\t"
          number.format(loanBalance)   "\n");
        
    } // for loop for writing data to text file
    outputFile.close();
    System.out.print("File Created");
        
} // saveReport
} // class

CodePudding user response:

I don't see an answer yet, maybe the way you are printing the values along the getNumberOfPayments() value, but correct me if I'm wrong: you are using the loanBalance variable before assigning it a value.

CodePudding user response:

This answer was in the comments so I don't think I could use it as an official answer, credit goes to user16320675.

Basically, I didn't initialize the loanBalance variable, so it was iterating into the negative numbers from zero, instead of decreasing from the starting balance.

  • Related