# Java Mortgage Calculator Help Needed ASAP!

Page 1 of 1

## 8 Replies - 1164 Views - Last Post: 01 October 2011 - 01:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=249403&amp;s=8ccac4c7b7979ede7292ce236357b22d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 javanoob82

Reputation: 0
• Posts: 41
• Joined: 03-August 09

# Java Mortgage Calculator Help Needed ASAP!

Posted 01 October 2011 - 12:16 PM

Hello! I need help badly with my Java homework. I will post the assignment requirements, and under the requirements will be the current code I have written...

REQUIREMENTS:

Write the program in Java (without a graphical user interface) and have it calculate the payment amount for 3 mortgage loans.

7 years at 5.35%

15 years at 5.5%

30 years at 5.75%

Use an array for the different loans. Display the mortgage payment amount for each loan and then list the loan balance and interest paid for each payment over the term of the loan. Use loops to prevent lists from scrolling off the screen.

Important Note:

1. The program should require two user input values.

2. The first input value is the loan amount.

3. The second input value is an indicator for displaying the balance and interest paid list.

4. Error checking should be done on the input loan amount to make sure that it is a positive number and is not zero.

5. If the input loan amount is wrong, show an error message and exit the program.

6. If the input loan amount is correct, proceed to compute.

7. If the indicator is not to show the list, then only show the mortgage payments.

8. If the indicator is to show the list, the program should show the mortgage payments first as in step 7. Following the mortgage payments, the list should be shown in 7 columns. The column are:

Column 1 - payment number

Column 2 – the first loan balance

Column 3 – the first loan interest paid

Column 4 – the second loan balance

Column 5 – the second loan interest paid

Column 6 – the third loan balance

Column 7 – the third loan interest paid

HERE IS MY CURRENT CODE:

```import java.io.*;    //This is the Java API Library Input/Output package (Data streams, Filesystem, Serialization)
import java.util.*;  //This is the Java API Library Utilities package (Collections, Event model, Date/time, International support)
import java.text.*;  //This is the Java API Library Text Utitilies package (Text, Dates, Numbers, Messages)

public class MullinsWeek3  //This begins the Calculator class. Public and class are keywords, while MullinsWeek3 is the identifier.
{
public static void main(String[] args)  //Defines the method of the main class.
{

//This declares the variables and defines the hard coded values.  This also begins the array.
double principal = 200000.00;								//Loan amount of the mortgage
double[] rate = {0.0535, 0.055, 0.0575};					//Interest rates of the mortgage based on the terms, respectively
int[] term = {7, 15, 30};									//Number of years for the mortgage, respectively
DecimalFormat twoDigits = new DecimalFormat("\$000.00");

//This is the first output that displays the loan amount of \$200,000
System.out.println("LOAN AMOUNT " + twoDigits.format(principal));
System.out.println();		//This creates a blank or new line

for (int i = 0; i < rate.length; i++)					//Loops through payments
{

//This is the second and third outputs that displays the term (in years) and annual interest rate
System.out.println("TERM " + term[i] + " YEARS");
System.out.println("ANNUAL INTEREST RATE " + rate[i]);

double payment = (principal*(rate[i]/12))/(1-(Math.pow(1/(1+(rate[i]/12)),(term[i]*12))));		//Mathematical calculation

//This is the fourth output that displays the mortgage payment
System.out.println("MONTHLY PAYMENT " + twoDigits.format(payment));
System.out.println();		//This creates a blank or new line

}		//This ends the loop

}			//This ends the class "main"

}			//This ends the class "MullinsWeek3"

```

I am aware that I am missing the user input portion and I am completely lost. Can someone please help me with this? It is due tonight and I'm not doing so well in my class. I have trouble grasping this stuff. I would appreciate any help. Thank you so much.

Is This A Good Question/Topic? 0

## Replies To: Java Mortgage Calculator Help Needed ASAP!

### #2 jon.kiparsky

• Pancakes!

Reputation: 8763
• Posts: 15,082
• Joined: 19-March 11

## Re: Java Mortgage Calculator Help Needed ASAP!

Posted 01 October 2011 - 12:34 PM

Use java.util.Scanner (documentation available here)

Print a prompt, like "Please enter the loan amount"
Use the scanner to get the next input. Either use Scanner.nextLine() and parse the input to a double (Double.parseDouble() is the method you need for this) or use Scanner.nextDouble(). Whichever method you use, assign the return to your principal variable, which you've already got. Same process for getting the indicator, except this time you want a boolean. Easiest thing is to enter "y" or "n" and use an if statement to set the indicator flag.

From there, you should be good.

This post has been edited by jon.kiparsky: 01 October 2011 - 12:35 PM

### #3 javanoob82

Reputation: 0
• Posts: 41
• Joined: 03-August 09

## Re: Java Mortgage Calculator Help Needed ASAP!

Posted 01 October 2011 - 12:42 PM

jon.kiparsky, on 01 October 2011 - 01:34 PM, said:

Use java.util.Scanner (documentation available here)

Print a prompt, like "Please enter the loan amount"
Use the scanner to get the next input. Either use Scanner.nextLine() and parse the input to a double (Double.parseDouble() is the method you need for this) or use Scanner.nextDouble(). Whichever method you use, assign the return to your principal variable, which you've already got. Same process for getting the indicator, except this time you want a boolean. Easiest thing is to enter "y" or "n" and use an if statement to set the indicator flag.

From there, you should be good.

Jon, thank you. I'm still not sure where I should place these pieces of code. My problem is I have the loan amount hard-coded. Would you mind pointing me in the right direction? I have yet to receive a reply from my instructor after 3 days, and this is very frustrating. Thanks again.

### #4 smohd

• Critical Section

Reputation: 1820
• Posts: 4,627
• Joined: 14-March 10

## Re: Java Mortgage Calculator Help Needed ASAP!

Posted 01 October 2011 - 12:42 PM

So which part of the code to help, getting user input? You need a Scanner class to use. Otherwise what is the specific problem?

### #5 smohd

• Critical Section

Reputation: 1820
• Posts: 4,627
• Joined: 14-March 10

## Re: Java Mortgage Calculator Help Needed ASAP!

Posted 01 October 2011 - 12:48 PM

So you will assign it value from user as what jon pointed.
First create a scanner object,
``` Scanner inp = new Scanner(System.in);//this means I can use inp to call methods from Scanner class
//Then ask for user input, and since it id double you will need inp.NextDouble(); and assign it to your principal.
principal = inp.NextDouble();//and like so user can input its value
```

### #6 javanoob82

Reputation: 0
• Posts: 41
• Joined: 03-August 09

## Re: Java Mortgage Calculator Help Needed ASAP!

Posted 01 October 2011 - 12:50 PM

smohd, on 01 October 2011 - 01:42 PM, said:

So which part of the code to help, getting user input? You need a Scanner class to use. Otherwise what is the specific problem?

The specific problem is that I do not know how to incorporate the user input portion. I'm not sure how to write the code. My textbook is very vague and I've been waiting for a reply from my instructor for the past 3 days. My code as it is now, works just fine, but I need to be able to manually enter a loan amount. The list of requirements in my original post are really confusing me. As far as writing the code, I have no idea how to modify my current code to match the requirements.

### #7 jon.kiparsky

• Pancakes!

Reputation: 8763
• Posts: 15,082
• Joined: 19-March 11

## Re: Java Mortgage Calculator Help Needed ASAP!

Posted 01 October 2011 - 01:14 PM

You can get the information at any point before you actually use it. Where exactly you put it in your code is pretty much up to you.

### #8 javanoob82

Reputation: 0
• Posts: 41
• Joined: 03-August 09

## Re: Java Mortgage Calculator Help Needed ASAP!

Posted 01 October 2011 - 01:29 PM

jon.kiparsky, on 01 October 2011 - 02:14 PM, said:

You can get the information at any point before you actually use it. Where exactly you put it in your code is pretty much up to you.

This is what I have now. Not sure where to put the scanner code, I'm just lost altogether. I appreciate the help.

```import java.util.Scanner
import java.io.*;    //This is the Java API Library Input/Output package (Data streams, Filesystem, Serialization)
import java.util.*;  //This is the Java API Library Utilities package (Collections, Event model, Date/time, International support)
import java.text.*;  //This is the Java API Library Text Utitilies package (Text, Dates, Numbers, Messages)

public class MullinsWeek3  //This begins the Calculator class. Public and class are keywords, while MullinsWeek3 is the identifier.
{
public static void main(String[] args)  //Defines the method of the main class.

Scanner inp = new Scanner(System.in);//this means I can use inp to call methods from Scanner class
//Then ask for user input, and since it id double you will need inp.NextDouble(); and assign it to your principal.
principal = inp.NextDouble();//and like so user can input its value

{

//This declares the variables and defines the hard coded values.  This also begins the array.
double principal = 200000.00;								//Loan amount of the mortgage
double[] rate = {0.0535, 0.055, 0.0575};					//Interest rates of the mortgage based on the terms, respectively
int[] term = {7, 15, 30};									//Number of years for the mortgage, respectively
DecimalFormat twoDigits = new DecimalFormat("\$000.00");

//This is the first output that displays the loan amount of \$200,000
System.out.println("LOAN AMOUNT " + twoDigits.format(principal));
System.out.println();		//This creates a blank or new line

for (int i = 0; i < rate.length; i++)					//Loops through payments
{

//This is the second and third outputs that displays the term (in years) and annual interest rate
System.out.println("TERM " + term[i] + " YEARS");
System.out.println("ANNUAL INTEREST RATE " + rate[i]);

double payment = (principal*(rate[i]/12))/(1-(Math.pow(1/(1+(rate[i]/12)),(term[i]*12))));		//Mathematical calculation

//This is the fourth output that displays the mortgage payment
System.out.println("MONTHLY PAYMENT " + twoDigits.format(payment));
System.out.println();		//This creates a blank or new line

}		//This ends the loop

}			//This ends the class "main"

}			//This ends the class "MullinsWeek3"

```

### #9 smohd

• Critical Section

Reputation: 1820
• Posts: 4,627
• Joined: 14-March 10

## Re: Java Mortgage Calculator Help Needed ASAP!

Posted 01 October 2011 - 01:39 PM

So you copy and paste it! That line should be after principal declaration, dont you see that the compiler will complain that the variable is not declared? you have first to DECLARE(without initializing them, because you intend to take value from user) variables then ask user to input value and assign them to the variable.
And again, you put that code between main() and open{ ?!

May be you should have a look at basic things in java. Look these tutorials:
Basic Java-How to use Scanner by NeoTifa
Basic Console IO by Locke

This post has been edited by smohd: 01 October 2011 - 01:46 PM