Results 1 to 14 of 14

# Thread: Need help please

- 12-19-2011, 03:50 AM #1Member
- Join Date
- Dec 2011
- Posts
- 15

- Rep Power
- 0

## Need help please

Hi.... I am in a java course and have no experience whatsoever with programming and am having trouble creating code for a mortgage calculator. It is supposed to be simple but I cannot figure it out for the life of me. I know I am missing something and probably quite a bit of code. Here is what is required and what I have: Calculate and display the monthly payment amount to fully amortize a $200,000.00 loan over a 30 year term at 5.75% interest.

Java Code:public class MortgageCalculator { NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); public static void main(String[] args) { } private double numOfPayments; double loanAmount = 200000; //This is the loan amount double loanLengthInYears = 30; //This is the length of the loan in years double annualInterest = 5.75; //This is the annual interest rate //Formuals bieng used are: double monthlyInterest = annualInterest/12; double monthlyPayment = (loanAmount * monthlyInterest) / (1 - Math.pow((1 + monthlyInterest), -numOfPayments)); System.out.println("MortgageCalculator"); System.out.println("Loan Amount: $200,000"); System.out.println("Loan Length: 30 Years"); System.out.println("Interest Rate: 5.75%"); }

Last edited by Eranga; 12-19-2011 at 04:06 AM. Reason: code tags added

- 12-19-2011, 04:06 AM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,372
- Blog Entries
- 1

- Rep Power
- 19

## Re: Need help please

What happen when compile and run your code?

- 12-19-2011, 04:14 AM #3Member
- Join Date
- Dec 2011
- Posts
- 15

- Rep Power
- 0

## Re: Need help please

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

- 12-19-2011, 04:16 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,372
- Blog Entries
- 1

- Rep Power
- 19

## Re: Need help please

Could you please post the complete error message?

Did you read it? There is some details about the line of the source code that error occur and so on.

- 12-19-2011, 04:18 AM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,372
- Blog Entries
- 1

- Rep Power
- 19

## Re: Need help please

Didn't you notice that the print statements and declarations, you have define outside of a method (outside of main method) which is illegal?

- 12-19-2011, 04:27 AM #6Member
- Join Date
- Dec 2011
- Posts
- 3

- Rep Power
- 0

## Re: Need help please

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

The last bit -numOfPayments seems that you are attempting to subtract your method from the equation?

That last subtraction should be the total term in months. You could get this by creating a new variable such as:

double termTotal = loanLengthInYears * 12;

This calculates the number of months of the loan- in this case it is 30 years times 12 months per year.

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

Another note is that*monthlyInterest*needs to be calculated and not put into that equation as a flat 5.75. You need to change this into the correct decimal format.

This can be done by dividing this 5.75 by 100 which gives us 0.0575 and then by 12 (month). You started to get this with*annualInterest*.

double rateCalc =*monthlyInterest*/100/12;

Take my advice with a grain of salt, I am new to Java and also in school myself.

- 12-19-2011, 04:43 AM #7Member
- Join Date
- Dec 2011
- Posts
- 15

- Rep Power
- 0

## Re: Need help please

Here is what I have now but still not getting it

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);

- 12-19-2011, 04:45 AM #8
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,372
- Blog Entries
- 1

- Rep Power
- 19

## Re: Need help please

As I said earlier, you cannot place print statements outside of a method in Java.

- 12-19-2011, 04:52 AM #9Member
- Join Date
- Dec 2011
- Posts
- 15

- Rep Power
- 0

## Re: Need help please

Can you explain that a bit more please... I dont know how to fix that. Ive only had 4 hours of class time so far and am trying to find what you are referring to in my book.

- 12-19-2011, 04:58 AM #10
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,372
- Blog Entries
- 1

- Rep Power
- 19

## Re: Need help please

You have one method in your class.

Java Code:public static void main(String[] args) { }

Java Code:public class MortgageCalculator { NumberFormat currencyFormat = NumberFormat.getCurrencyInstance(); public static void main(String[] args) { double numOfPayments = 0; double loanAmount = 200000; //This is the loan amount double loanLengthInYears = 30; //This is the length of the loan in years double annualInterest = 5.75; //This is the annual interest rate //Formuals bieng used are: double monthlyInterest = annualInterest/12; double monthlyPayment = (loanAmount * monthlyInterest) / (1 - Math.pow((1 + monthlyInterest), -numOfPayments)); System.out.println("MortgageCalculator"); System.out.println("Loan Amount: $200,000"); System.out.println("Loan Length: 30 Years"); System.out.println("Interest Rate: 5.75%"); } }

My suggestion is you to have a look at the basis in Java again. Seems you are messup with all.

- 12-19-2011, 08:24 AM #11Member
- Join Date
- Dec 2011
- Posts
- 15

- Rep Power
- 0

## Re: Need help please

Ok thank you! I think I have most of it figured out but I cant figure out the math portion of it. I am not getting the correct monthly payment. Here is my updated 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);

}

}

- 12-19-2011, 08:58 AM #12
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,372
- Blog Entries
- 1

- Rep Power
- 19

## Re: Need help please

It's all about the logic you have implemented. Have a look at it again and check that the correct logic is implemented or not.

- 12-19-2011, 09:16 AM #13Member
- Join Date
- Dec 2011
- Posts
- 15

- Rep Power
- 0

## Re: Need help please

Ok I got it and the correct monthly payment is being displayed.... One last thing Im trying to do is run a team mates project but it only runs my project which is the main project... How can I run another project. I am using NetBeans

- 12-19-2011, 10:20 AM #14
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,372
- Blog Entries
- 1

- Rep Power
- 19

## Re: Need help please

You mean run the multiple project in NetBeans?

## Bookmarks