Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

I have about 4 hours of class time with Java and have an assignment due. I cant figure out how to fix my code. I know Im having issues with my print statements but I just dont understand it. Here is what I have and what is required: Calculate and display the monthly payment amount to fully amortize a \$200,000.00 loan over a 30 year term at 5.75% interest. Please help.

package mortgagecalculator;

public class MortgageCalculator {
public static void main(String[] args) {

}

double annualInterest = 5.75; //This is the annual interest rate
double loanAmount = 200000; //This is the loan amount
double loanLengthInYears = 30; //This is the length of the loan in years

//Formuals bieng used are:
double monthlyInterest = annualInterest / 100 /12;
double loanLengthInMonths = loanLengthInYears * 12;

double monthlyPayment = (loanAmount * monthlyInterest) / (1 - Math.pow((1 + monthlyInterest), loanLengthInYears));

System.out.println("MortgageCalculator");
System.out.println("Annual Interest: 5.75%");
System.out.println("Loan Amount: \$200,000");
System.out.println("Loan Length: 30 years");
System.out.println("Monthly Payment: " + currencyFormat.format(monthlyPayment));

2. ## Re: Need help please

What is currencyFormat.format. Are they custom class and method? If they are you will need to post the code. I think what you are looking for is the String.format
method. Take a look at this link: Java Platform SE 7 look at the format method.

Hope it helps

3. ## Re: Need help please

Would it make a difference if that last println statement was replaced like this:

package mortgagecalculator;

/**
*
* @author Johnny
*/
public class MortgageCalculator {
public static void main(String[] args) {

}

double annualInterest = 5.75; //This is the annual interest rate
double loanAmount = 200000; //This is the loan amount
double loanLengthInYears = 30; //This is the length of the loan in years

//Formuals bieng used are:
double monthlyInterest = annualInterest / 100 /12;
double loanLengthInMonths = loanLengthInYears * 12;

double monthlyPayment = (loanAmount * monthlyInterest) / (1 - Math.pow((1 + monthlyInterest), loanLengthInYears));

System.out.println("MortgageCalculator");
System.out.println("Annual Interest: 5.75%");
System.out.println("Loan Amount: \$200,000");
System.out.println("Loan Length: 30 years");
System.out.println(monthlyPayment);

4. ## Re: Need help please

Originally Posted by kprofgold
Would it make a difference if that last println statement was replaced like this:

package mortgagecalculator;

/**
*
* @author Johnny
*/
public class MortgageCalculator {
public static void main(String[] args) {

}

double annualInterest = 5.75; //This is the annual interest rate
double loanAmount = 200000; //This is the loan amount
double loanLengthInYears = 30; //This is the length of the loan in years

//Formuals bieng used are:
double monthlyInterest = annualInterest / 100 /12;
double loanLengthInMonths = loanLengthInYears * 12;

double monthlyPayment = (loanAmount * monthlyInterest) / (1 - Math.pow((1 + monthlyInterest), loanLengthInYears));

System.out.println("MortgageCalculator");
System.out.println("Annual Interest: 5.75%");
System.out.println("Loan Amount: \$200,000");
System.out.println("Loan Length: 30 years");
System.out.println(monthlyPayment);
Does this program even compiling? If no, paste here the errors/exceptions.

5. ## Re: Need help please

I get cannot find symbol, identifier expected, and illegal start of type errors

6. ## Re: Need help please

Can you copy the errors and paste here?

7. ## Re: Need help please

I think this is the error Im not very good at this sorry.

Not able to submit breakpoint LineBreakpoint MortgageCalculator.java : 81, reason: The breakpoint is set outside of any class.
Invalid LineBreakpoint MortgageCalculator.java : 81
User program running
User program finished

8. ## Re: Need help please

Well, i don't know why aren't you trying to understand what i mean to say.
Simply,
```double annualInterest = 5.75; //This is the annual interest rate
double loanAmount = 200000; //This is the loan amount
double loanLengthInYears = 30; //This is the length of the loan in years

//Formuals bieng used are:
double monthlyInterest = annualInterest / 100 /12;
double loanLengthInMonths = loanLengthInYears * 12;

double monthlyPayment = (loanAmount * monthlyInterest) / (1 - Math.pow((1 + monthlyInterest), loanLengthInYears));

System.out.println("MortgageCalculator");
System.out.println("Annual Interest: 5.75%");
System.out.println("Loan Amount: \$200,000");
System.out.println("Loan Length: 30 years");
System.out.println(monthlyPayment);```
paste this piece of code inside main() and then check the closing and opening braces.

9. ## Re: Need help please

Ok thank you... I was closing the code before I had even inputted anything and I had to restart my NetBeans for the code to run. But now the calculation is off and I think it may be within the math. Here are my results:

MortgageCalculator
Annual Interest: 5.75%
Loan Amount: \$200,000
Loan Length: 30 years
-6214.908191559089 (I know this should a positive # lol)

10. ## Re: Need help please

`(1 - Math.pow((1 + monthlyInterest), loanLengthInYears))`
It's giving the negative value.

11. ## Re: Need help please

I still havnt been able to fix the math yet.... but I noticed I am also getting a gray line under my loanLengthInMonths...the error is not a known variable in the current context

12. ## Re: Need help please

but I noticed I am also getting a gray line under my loanLengthInMonths
It's not an error but it is coz the variable loanLengthInMonths is not being used anywhere after assigning value to it.

Also, i don't know what you are supposed to do, might be the problem in your formulas.

13. ## Re: Need help please

Here is what I am supposed to calculate: Calculate and display the monthly payment amount to fully amortize a \$200,000.00 loan over a 30 year term at 5.75% interest. And thank you for all of your help.

14. ## Re: Need help please

I think I just figured it out. Heres the code

public class MortgageCalculator {

public static void main(String[] args) {
double annualInterest = 5.75; //This is the annual interest rate
double loanAmount = 200000; //This is the loan amount
double loanLengthInYears = 30; //This is the length of the loan in years

//Formuals bieng used are:
double monthlyInterest = annualInterest /100/12;
double loanLengthInMonths = loanLengthInYears * 12;

double monthlyPayment = (loanAmount * monthlyInterest) * (1 + Math.pow((1 - monthlyInterest), loanLengthInYears));

System.out.println("MortgageCalculator");
System.out.println("Annual Interest: 5.75%");
System.out.println("Loan Amount: \$200,000");
System.out.println("Loan Length: 30 years");
System.out.println(monthlyPayment);
}
}

Good Luck.