Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java loan calculator program using Arrays

807600Oct 20 2007 — edited Oct 20 2007
I was wondering if some of the more experienced Java programmers can show me what is wrong with my code? I cannot get the loan details for my second loan and third loan. The program only displays the first loan amount. I am new to java would like to learn arrays but have problems understanding the array logic concepts. Any help is appeciated!
import java.text.DecimalFormat;

class Mortgage5 {

public static void main(String args[]) throws Exception

{
	// Declare variables
	int 	loanAmt = 200000; // Principal loan amount
	int	 [] loanTerm = {84, 180 ,360}; // Loan term for 7 years, 15 years, and 30 years
	double[] intRate = {5.35, 5.50, 5.75}; // Interest rates for 7 year, 15 year, and 30 year loans
	int monthNum = 85;
	int monthNum2 = 181;
	int monthNum3 = 361;
	int loanNum = 0;
	int line = 0;
	double monthlyPay = 0; // Displays monthly payment calculation
	double newLoanBal = 200000; // Loan balance
	double monIntPaid = 0; // Interest paid
	double newIntRate = 0; // Monthly interest rate
	double monPrinPay; // Monthly principal payment
	DecimalFormat money = new DecimalFormat("$0.00"); // Displays mortgage amount in decimal format
	DecimalFormat interest = new DecimalFormat("0.00%");// Displays interest rate amount in decimal format

	// Display messages in the console window
	System.out.println();
	System.out.println("Week 5 Mortgage Payment Calculator");
	System.out.println();
	System.out.println("This Java program will calculate three separate mortgage payments for a $" + loanAmt);
	System.out.println("with the following loan terms and interest rates:");
	System.out.println(" 7 years @ 5.35%");
	System.out.println("15 years @ 5.50%");
	System.out.println("30 years @ 5.75%");
	System.out.println();
	System.out.println("Following the math calculation of the payments, the program will display the");
	System.out.println("mortgage payments, interest paid, and loan balance for the terms of the");
	System.out.println("of the three different loans.");
	System.out.println();
	System.out.println("The loan results are as follows:");

	int i;

	for (i = 0; i <= 2; i++)
	{

	int j;

	for (j = 0; j <= 2; j++)
	{

	// Performs calculation for loan term, interest rate, and monthly payment
	loanTerm[i] = loanTerm;
intRate[i] = (intRate[i] * .01) / 12;
monthlyPay = loanAmt * intRate[i] / (1 - Math.pow(1 + intRate[i], - loanTerm[i]));



if (loanNum <= 2)

{

loanNum++;

// Display results for each loan in the console window
System.out.println();
System.out.println("Loan " + loanNum);
System.out.println("**************************************************************************");
System.out.println();
System.out.println("The monthly mortgage payment for a $" + loanAmt + " over a " + loanTerm[i]/12 + "-year period at a " + (interest.format(intRate[i] * 12)));
System.out.println("interest rate = " + (money.format(monthlyPay)));
System.out.println();

System.out.println("The mortgage payment, interest paid, and loan balance for the loan is as");
System.out.println("follows:");
System.out.println();

}

// Begins loop
while(newLoanBal > 0)
{

if (i >= 0)

monIntPaid = intRate[i] * newLoanBal;
monPrinPay = monthlyPay - monIntPaid;
newLoanBal = newLoanBal - monthlyPay + monIntPaid;

// Displays results of calculations for monthly interest paid, monthly principal paid, and new loan balance
System.out.println((money.format(monthlyPay)) + "\t" + (money.format(monIntPaid)) + "\t\t" + (money.format(newLoanBal)) + "\tPayment(s) completed.");

// Decrements monthly count one month at a time until count reaches zero
monthNum--;


// Pause console window, then continue calculations
if (line == 20)
{
line = 0;
try
{
Thread.sleep(2000);
}
catch (InterruptedException e)
{
}
}
}
}
}
}
}
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 17 2007
Added on Oct 20 2007
7 comments
1,775 views