package mortgagecalculator; /** this calculates the monthly payments and mortization tables for mortgages * * @author Melissa Foulger 12/09 */ import java.io.*; //accepts user input import java.text.NumberFormat; //formats decimal values public class MortgageCalculator { /** * @param args the command line arguments */ public static void main(String[] args)throws Exception { System.out.println("This program calculates monthly payments " + "and amoritization tables"); BufferedReader input = new BufferedReader (new InputStreamReader (System.in) ); String inputstring; //allocates space for user input double Principle, Interest, months; //declares variables NumberFormat dollars = NumberFormat.getCurrencyInstance();// set formats NumberFormat percent = NumberFormat.getPercentInstance(); percent.setMaximumFractionDigits(2); System.out.println("Enter Principle Amount and press enter"); inputstring = input.readLine(); Principle = Double.parseDouble(inputstring); //gets principle from user System.out.println("Principle Loan Amount: " + dollars.format (Principle)); System.out.println("Enter Annual Interest and press enter"); inputstring = input.readLine(); //gets rate from user Interest = ((Double.parseDouble((inputstring)) *.01 / 12)); //annual 2 monthly System.out.println("Enter Length of Loan in Years and press enter"); inputstring = input.readLine(); //gets length from user in years months = Double.parseDouble(inputstring) * 12; //years 2 months double monthly_payments = (Principle * Interest) /(1- Math.pow( 1 + Interest , -months)); //calculates the monthly payments System.out.println("The monthly payments will be: " + dollars.format (monthly_payments)); double interest_paid = (Principle * Interest); double principle_paid = (monthly_payments - interest_paid); double ending_balance = Principle - principle_paid; int count = 0; while (count < 360) { double new_balance = ending_balance; interest_paid = new_balance * (Interest); // Calculate interest by multiplying rate against balance principle_paid = monthly_payments - interest_paid; // Subtract interest from your payment ending_balance = new_balance - principle_paid; // Subtract final payment from running balance // loan, interest, and payment made towards principle System.out.println ( " Payment: " + dollars.format(monthly_payments)); System.out.println( " Interest: " + dollars.format(interest_paid)); System.out.println( " Principle: " + dollars.format(principle_paid)); System.out.println( " Loan Balance is: " + dollars.format(ending_balance)); System.out.println("Press enter to continue"); try { System.in.read(); } catch (IOException e) { count ++; } if (count > 360) { //when all payments have been made. System.out.println (" Payment: " + dollars.format(new_balance + interest_paid) + " Interest: " + dollars.format(interest_paid) + " Principle: " + dollars.format(new_balance - interest_paid) + " Loan Balance is: $0.00"); } } } } [/code} Then I have this to create the array but I'm not sure where it would go or how I get the amounts into it. [code] double [] monthlyPayments = new double [3]; //declares the array int cnt = 0; for(cnt = 0; cnt < monthlyPayments.length; cnt++) { monthlyPayments[cnt]=cnt;

# Need help with assigning values to an array

Page 1 of 1## 1 Replies - 1016 Views - Last Post: 18 December 2009 - 08:11 AM

### #1

# Need help with assigning values to an array

Posted 18 December 2009 - 07:54 AM

I'm new at this and I'm having some trouble figuring out how to assign values to an array once I've figured out the values. Maybe I'm trying to do something that can't be done. I need to get the monthly payment amounts for three different mortgages. This is the code I have so far:

##
**Replies To:** Need help with assigning values to an array

### #2

## Re: Need help with assigning values to an array

Posted 18 December 2009 - 08:11 AM

girlinmorgue, on 18 Dec, 2009 - 06:54 AM, said:

I'm new at this and I'm having some trouble figuring out how to assign values to an array once I've figured out the values. Maybe I'm trying to do something that can't be done. I need to get the monthly payment amounts for three different mortgages. This is the code I have so far:

double [] monthlyPayments = new double [3]; //declares the array int cnt = 0; for(cnt = 0; cnt < monthlyPayments.length; cnt++) { monthlyPayments[cnt]=cnt;

I think you're almost there. You assign a value to an array by picking the index and then assigning a value of the type you specified when you declared the array (i.e. if you said it's an array of doubles, assign a double).

ex:

int[] example = new int[5];

example[0] = 42;

example[1] = 43;

System.out.println("The ultimate answer is " +example[0]);

Hence in your code:

monthlyPayments[cnt]=cnt;

Just means that monthlyPayments[0] = 0.

But what you probably want is monthly payment to be what you're calculating up top:

double monthly_payments = (Principle * Interest) /(1- Math.pow( 1 + Interest , -months)); //calculates the monthly payments System.out.println("The monthly payments will be: " + dollars.format (monthly_payments)); monthlyPayments[someIndex] = monthly_payments;

Now what I can't tell you is exactly how you want to apply that loop because I'm not 100% sure what you're trying to get at. There's some logical relationship between what you're calculating and months, but you'll have to figure that out based on your goals.

However I hope the array bit was straightforward.

Page 1 of 1