Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
• Status: Solved
• Priority: Medium
• Security: Public
• Views: 275

# I am trying loop mortgage payments

import java.text.*;

public class mortgage
{
//private member variables declared static for now

double loanamount  = 200000.00;      //Total mortgage payment
double loanterm    = 30.00;      //Length of years for loan
double interestrate  = 0.0575;      //Interest rate for loan

double monthlyprinciple;
double loanbalance = 0.0;
double monthinterest;

int payment = 0;
int months = 0;

void main(String[] args)  //Entry point. Only one can exist per class and invocation.
{

double monthlyinterest = interestrate/12;      //Months in a year for loan
double monthlyterm = loanterm*12;      //Monthly precentage rate

//convert to monthly terms
double monthlytotal=(Math.pow((1+monthlyinterest),monthlyterm)-1)/(monthlyinterest*Math.pow((1+monthlyinterest),monthlyterm));
double monthlypayment = loanamount/monthlytotal;

//Format values as currency for the default locale.
String monthlypaymenttotal = java.text.NumberFormat.getCurrencyInstance().format(monthlypayment);
String totalloanamount = java.text.NumberFormat.getCurrencyInstance().format(loanamount);

//Output the results by field.
System.out.println("-----------------------------------------------------------");
System.out.println("Total Loan Amount: " + totalloanamount);
System.out.println("Interest Rate: " + 100 * interestrate);
System.out.println("Loan Term: " + loanterm);
System.out.println("-----------------------------------------------------------");
System.out.println("Total Monthly Payment: " + monthlypaymenttotal);

while (loanamount >= 0.01) //Loop ends when loan is below one penny
{   //loop loan payment
monthlyinterest = (loanamount * interestrate);
monthlyprinciple = monthlypayment - monthinterest;
loanbalance = loanamount - monthlyprinciple;   //calculates new principal
loanamount = loanbalance;
payment++;
months++;
if (months == 6)
{   //pauses to show payments
pause(3600);
months= 0;
}
System.out.println("\n\nPayment Number:" + payment+ "\n------------"+ "\nTotal Loan Balance = " + form.format(loanamount) + "\nAmount of Interest Paid = " + form.format(monthinterest));

}
}

public static void pause(int duration) {   //sets up pause object
try
{
}
catch (InterruptedException e) {}
}
}

Error
mortgage.java:74: cannot resolve symbol
symbol  : variable form
location: class mortgage
System.out.println("\n\nPayment Number:" + payment+ "\n
-----------"+ "\nTotal Loan Balance = " + form.format(loanamount) + "\nAmount o
Interest Paid = " + form.format(monthinterest));

^
mortgage.java:74: cannot resolve symbol
symbol  : variable form
location: class mortgage
System.out.println("\n\nPayment Number:" + payment+ "\n
-----------"+ "\nTotal Loan Balance = " + form.format(loanamount) + "\nAmount o
Interest Paid = " + form.format(monthinterest));

^
2 errors

E:\Documents and Settings\sublime\Desktop\University of Phoenix\POS_406\Program
>
0
sublime76
• 3
1 Solution

Senior Developer/ArchitectCommented:

NumberFormat form = NumberFormat.getCurrencyInstance();

just before the System.out.println referencing form on line 74.

BTW, when you try to test this you'll find that it won't run (or I don't think it will) because the main method must be defined as public static. Change your class like this and you'll get better results:

Replace:

void main(String[] args) {

with:

public static void main(String[] args) {
new Mortgage().run();
}

public void run() {

Best regards,
Jim Cakalic
0

Senior Developer/ArchitectCommented:
Oh, yeah -- make sure to add an import of java.text.NumberFormat. Eclipse does it for me so I sometimes forget to make it part of my post.

Jim
0

Author Commented:
Thanks but I already fixed it.  Here is what the program needs to look like.  I do appreciate you help though.

import java.text.*;

class mortgage
{
//private member variables declared static for now

public static void main(String[] args)  //Entry point. Only one can exist per class and invocation.

{

double loanamount  = 200000.00;      //Total mortgage payment
double loanterm    = 30.00;      //Length of years for loan
double interestrate  = 0.0575;      //Interest rate for loan

double monthlyprinciple;
double loanbalance = 0.0;
double monthinterest;

int payment = 0;
int months = 0;

double monthlyinterest = interestrate/12;      //Months in a year for loan
double monthlyterm = loanterm*12;      //Monthly precentage rate

NumberFormat form = NumberFormat.getCurrencyInstance();   //creates form object

//convert to monthly terms
double monthlytotal=(Math.pow((1+monthlyinterest),monthlyterm)-1)/(monthlyinterest*Math.pow((1+monthlyinterest),monthlyterm));
double monthlypayment = loanamount/monthlytotal;

//Format values as currency for the default locale.
String monthlypaymenttotal = java.text.NumberFormat.getCurrencyInstance().format(monthlypayment);
String totalloanamount = java.text.NumberFormat.getCurrencyInstance().format(loanamount);

//Output the results by field.
System.out.println("-----------------------------------------------------------");
System.out.println("Total Loan Amount: " + totalloanamount);
System.out.println("Interest Rate: " + 100 * interestrate);
System.out.println("Loan Term: " + loanterm);
System.out.println("-----------------------------------------------------------");
System.out.println("Total Monthly Payment: " + monthlypaymenttotal);

while (loanamount >= 0.01) //Loop ends when loan is below one penny
{   //loop loan payment
monthinterest = (loanamount * interestrate);
monthlyprinciple = monthlypayment - monthinterest;
loanbalance = loanamount - monthlyprinciple;   //calculates new principal
loanamount = loanbalance;
payment++;
months++;
if (months == 6)
{   //pauses to show payments
pause(3600);
months= 0;
}
System.out.println("\n\nPayment Number:" + payment+ "\n------------"+ "\nTotal Loan Balance = " + form.format(loanamount) + "\nAmount of Interest Paid = " + form.format(monthlyinterest));

}
}

public static void pause(int duration) {   //sets up pause object
try
{
}
catch (InterruptedException e) {}
}
}
0

Senior Developer/ArchitectCommented: