Hi,
I am hoping someone can help me get this cleaned up.
My calculations are working fine but I can't seem to get the format right.
I would like to break down each loan display by year and month, ie.
Yr 1
Month
1
2
3
.
.
6
Yr2
Month
blah
blah
and so on.
One of my problems has been that it is taking the term data from the array. I was able to get this to work fine when the term value was hard coded, but I haven't been able to get this part figured out since I started taking the value assignments from the arrays.
Before the array implementation I had it set up to display a year and it's 12 months, paused, and then started again with the next year.
Anyway, I'd appreciate any advice anyone could offer.
Thank you,
Dan
P.S. I am currently getting each payment listed one line after another without any break. Also the only val I am currently getting in the month column is the total months -1.
Code:
import java.text.*;
import java.util.*;
import java.lang.*;
import java.io.*;
public class MCalcv5
{
public static void main(String[] args)
{
double monthlyPayment;//monthly payment calculation (includes interest)
double []principal={200000,200000,200000};//principal amount, same for all three loans
double []interestRateYears={5.35,5.5,5.75};//annual interest rate
int []termYears={7,15,30};//length of loan in years
double interestPaid;//accumulator for total interest over whole loan
double interest;//monthly interest payment
double monthlyPrincipalPayment;//monthly payment to principal only
double totalPaid;//accumulator for total payments over whole loan
for (int i=0;i<principal.length; i++)//loop to run until pricipal has expired
{
double loan=principal[i];//set var loan equal to val in array
double prt=interestRateYears[i];//set var prt equal to val in array
double interestRateMonths = (interestRateYears[i] / 1200);//calculate monthly interest rate from annual
int termMonths = (termYears[i] * 12);//calculate months from years
monthlyPrincipalPayment = 0;//monthly payment excluding interest
double balance = principal[i];//set balance from principal
interest = 0;
interestPaid = 0;
totalPaid = 0;
int x = termYears[i];
{
for (int y = 0; y < x; y++);//loop to calculate display monthly values
{
DecimalFormat dec = new DecimalFormat(", ###.00 ");//needed to output currency
termMonths--;
System.out.println("\n\n\t*** Mortgage Calculator***\n\n"+//display column headings
"\nLoan "+(i+1)+//
"\nLoan Amount: \t$" + dec.format(principal[i]) +//
"\nInterest Rate: \t" + interestRateYears[i] +"%" +//
"\nTerm (years): \t" + termYears[i]);//
monthlyPayment = (loan * interestRateMonths)/
(1-Math.pow(1+ interestRateMonths, - termMonths));//calculate monthly payment
System.out.println("\n Monthly payment = " + "$" + dec.format (monthlyPayment));
System.out.println("\n\nMonths\tPrincipal Paid\t\tInterest Paid\t\tBalance");
System.out.println("------\t---------\t\t--------\t\t-------\t");
{
for(int k = 0; k<=termMonths; k++)
{
interest= balance * interestRateMonths; //formula for monthly intrest
interestPaid=interestPaid+interest; //totals interest paid
monthlyPrincipalPayment = monthlyPayment-interest; //subtracts interest from principal
balance= balance - monthlyPrincipalPayment; //subtracts principal from balance
totalPaid= interestPaid + principal[i]; //displays total interest and original loan balance
System.out.println(termMonths + "\t$"+dec.format(monthlyPrincipalPayment)+"\t\t$"+
dec.format(interest) +"\t\t$"+ dec.format(balance));
}
try //for display delay
{
Thread.sleep(200); //
}
catch (InterruptedException e) //
{
}
}
System.out.println("\n Total Interest Paid = " + "$" + dec.format (interestPaid)); //total interest paid
System.out.println("\n Total Amount Paid ="+"$"+ dec.format (totalPaid)); //total amount paid
}
}
}
}
}