# Loop Help

• February 20th, 2012, 03:00 PM
rushd
Loop Help
I am working on a project for school and I am not sure the proper way to loop the program. I have to do separate interest rates and and term years. I have built those two vars into arrays but can't seem to get it to work right. I am sure I am using them wring and should just be looping the entire payment call, but not sure how to do it.

Code Java:

```  import java.text.*; //To use text formatting import java.util.Scanner; //To use the scanner   public class Wk1MortgagePaymentCalculator {   NumberFormat currency = NumberFormat.getCurrencyInstance(); //Currancy Format DecimalFormat intRate = new DecimalFormat("##.00"); //Interest Format   /** * The following loads the static variables for the program. */   double payment = 0.0; double rate = 0.0; double principal = 200000.0; double[] annualInterest = {0.0535,0.0550,0.0575}; int[] years = {7,15,30}; int months = 0; int lineCount = 20;   public static void main(String[] args) { Wk1MortgagePaymentCalculator MP = new Wk1MortgagePaymentCalculator(args);   }   public Wk1MortgagePaymentCalculator(String[] args) { if (args.length == 3) { principal = Double.parseDouble(args[0]); for (int i=0; i< annualInterest.length; i++) annualInterest[i] = Double.parseDouble(args[1])/100; for (int i=0; i< years.length; i++) years[i] = Integer.parseInt(args[2]); print(); } else { showUsage(); }   }   /** * The following calculates the monthly payment. */   public void calculatePayment(){ for (int i=0; i< annualInterest.length; i++) rate = annualInterest[i] / 12; for (int i=0; i< years.length; i++) months = years[i] * 12; payment = (principal * rate) / (1 - Math.pow(1/ (1 + rate), months));   }   /** * The following prints out information about the program, the static * fields, and calls the amortization. */   public void print(){ System.out.println("The principal is " + currency.format(principal)); for (int i=0; i< annualInterest.length; i++) System.out.println("The annual interest rate is " + intRate.format(annualInterest[i] * 100) +"%"); for (int i=0; i< years.length; i++) System.out.println("The term is " + years[i] + " years"); System.out.println("Your monthly payment is " + currency.format(payment)); //showAmortization(); // Calls the amortization method }   public void showUsage() { calculatePayment(); // Calls the calculate payment method System.out.println("Usage: Calculate Mortgage Payment Amount "); System.out.println("\nFor example: \$200,000 Mortgage loan amount, " + "5.75% interest for a 30 year term "); System.out.println("\nThe Following is your output based on the " + "hardcoded amount: \n"); print(); System.exit(0); // Controlled exit   }   /** * Prints out the amortization table for the loan. */   public void showAmortization() { double pmtInterest = 0.0; double pmtPrincipl = 0.0; double pmtBalance = 0.0;     System.out.println("\nPmt#:\tPrincipalPmt\tInterestPmt\tBalance"); System.out.println("====\t===========\t===========\t=======");   Scanner s = new Scanner(System.in);   for ( int i = 1; i <= months; ++i ) { pmtInterest = principal * rate; // Payment to intrest pmtPrincipl = payment - pmtInterest; // Payment to princapl pmtBalance = principal - pmtPrincipl;   System.out.println( "#" + i + "\tP=" + currency.format(pmtPrincipl) + "\tI=" + currency.format(pmtInterest) + (pmtInterest < 10 ? "\t\tB=" : "\tB=") // Tab adjustment + currency.format(pmtBalance) );   principal -= pmtPrincipl; // Reduce the payment-to-principal   if ( (i % lineCount) == 0 ) { String input = s.nextLine(); // Pauses for an enter   }   }   s.close(); // Closes the scanner call   }     }```
• February 20th, 2012, 07:50 PM
Norm
Re: Loop Help
You need to properly format your code to make it easier to read and understand.
Statements within for statements should be indented and enclosed in {}s

Can you explain what your problem with looping is? Explain what the loop is supposed to do on each time around.
• February 21st, 2012, 08:13 AM
rushd
Re: Loop Help
Norm,

I have arrays built for the interest rate and term of a loan (annualInterest & years). What I believe I need to do but not sure how is to loop calculatePayment, print, and showAmortization to go through each index of the two arrays.

Don
• February 21st, 2012, 08:25 AM
Norm
Re: Loop Help
Can you explain what you want to do?
For any loan I would assume that there is one interest rate and one term of years.

Pick an interest rate and then compute the values for all the terms of years.
Then pick the next interest rate and compute the values for all the terms of years.
Looks like too loops> one to pick the intertst rates and then to use the years
• February 21st, 2012, 03:08 PM
rushd
Re: Loop Help
I do not need to calculate a given interest rate for all terms. I need to do index 0 of each var, index 1 and then index 2.

Once the program runs against annualInterest[0] & years[0], I need it to move to annualInterest[1] & years[1].

Here is the running code without some loops I was playing with.

Code Java:

```  import java.text.*; //To use text formatting import java.util.Scanner; //To use the scanner   public class Wk1MortgagePaymentCalculator {   NumberFormat currency = NumberFormat.getCurrencyInstance(); //Currancy Format DecimalFormat intRate = new DecimalFormat("##.00"); //Interest Format   /** * The following loads the static variables for the program. */   double payment = 0.0; double rate = 0.0; double principal = 200000.0; double[] annualInterest = {0.0535,0.0550,0.0575}; int[] years = {7,15,30}; int months = 0; int lineCount = 20;   public static void main(String[] args) { Wk1MortgagePaymentCalculator MP = new Wk1MortgagePaymentCalculator(args);   }   public Wk1MortgagePaymentCalculator(String[] args) { if (args.length == 3) { principal = Double.parseDouble(args[0]); annualInterest[0] = Double.parseDouble(args[1])/100; years[0] = Integer.parseInt(args[2]); print(); } else { showUsage(); }   }   /** * The following calculates the monthly payment. */   public void calculatePayment(){ rate = annualInterest[0] / 12; months = years[0] * 12; payment = (principal * rate) / (1 - Math.pow(1/ (1 + rate), months));   }   /** * The following prints out information about the program, the static * fields, and calls the amortization. */   public void print(){ System.out.println("The principal is " + currency.format(principal)); System.out.println("The annual interest rate is " + intRate.format(annualInterest[0] * 100) +"%"); System.out.println("The term is " + years[0] + " years"); System.out.println("Your monthly payment is " + currency.format(payment)); showAmortization(); // Calls the amortization method }   public void showUsage() { calculatePayment(); // Calls the calculate payment method System.out.println("Usage: Calculate Mortgage Payment Amount "); System.out.println("\nFor example: \$200,000 Mortgage loan amount, " + "5.75% interest for a 30 year term "); System.out.println("\nThe Following is your output based on the " + "hardcoded amount: \n"); print(); System.exit(0); // Controlled exit   }   /** * Prints out the amortization table for the loan. */   public void showAmortization() { double pmtInterest = 0.0; double pmtPrincipl = 0.0; double pmtBalance = 0.0;     System.out.println("\nPmt#:\tPrincipalPmt\tInterestPmt\tBalance"); System.out.println("====\t===========\t===========\t=======");   Scanner s = new Scanner(System.in);   for ( int i = 1; i <= months; ++i ) { pmtInterest = principal * rate; // Payment to intrest pmtPrincipl = payment - pmtInterest; // Payment to princapl pmtBalance = principal - pmtPrincipl;   System.out.println( "#" + i + "\tP=" + currency.format(pmtPrincipl) + "\tI=" + currency.format(pmtInterest) + (pmtInterest < 10 ? "\t\tB=" : "\tB=") // Tab adjustment + currency.format(pmtBalance) );   principal -= pmtPrincipl; // Reduce the payment-to-principal   if ( (i % lineCount) == 0 ) { String input = s.nextLine(); // Pauses for an enter   }   }   s.close(); // Closes the scanner call   }     }```
• February 21st, 2012, 03:15 PM
Norm
Re: Loop Help
Are you sure that you don't need to calculate the values for all three years for each of the interest rates? That would make for 9 different calculations.
You think you only need to do it for 3. That would mean you only need one loop
• February 22nd, 2012, 12:09 AM
rushd
Re: Loop Help
I am sure I only need three calculations. I am running short on turning this in. How would I get it to loop the entire program to run through each of the array index's?
• February 22nd, 2012, 07:23 AM
Norm
Re: Loop Help
Where does the code currently use elements from the arrays? Can that code be put in a loop that iterates through all the elements in the arrays instead of just using the one hardcoded [0]th element?