Solved

# Array loop not resetting

Posted on 2006-05-01
Medium Priority
337 Views
I am having a bit of problem resetting my values from my array back to default for the second and third run. Where must I fix it? I am new and this has consummed my life!!! lol.

import java.io.*;
import java.text.*;

public class MtgCalc5
{
public static void main(String[] args) throws IOException
{
DecimalFormat decimalPlaces=new DecimalFormat("0.00");

double monthlyPayment=0;
double principal;
double balance=0;
double monthlyInterestPayment=0;
double monthlyPrincipalPayment;
double interestRateMonths;
int termMonths = 0;
int linecount;

//assignment of Array values

Double interestRateYears[]={5.35, 5.5, 5.75};
int termYears[]={7,15,30};

//assignment of fixed values
principal = 200000.0;
balance = principal;                  // set current balance = principal
monthlyInterestPayment = 0;            // set current interest payment = 0
monthlyPrincipalPayment = 0;            // set current principal payment = 0
linecount = 50;                  // display 50 lines of results

for (int i = 0; i < interestRateYears.length; i++)
{

// calculation of monthly payment
principal = 200000.0;
termMonths = termYears[i] * 12;
interestRateMonths = (interestRateYears[i] / 12) / 100;
monthlyPayment = (principal * interestRateMonths) / (1 - Math.pow(1 + interestRateMonths, - termMonths));

// formating used to set column headers for results
System.out.println("\n\n\n\n");
System.out.println("\tWith an interest rate of" + (interestRateYears[i]));
System.out.println("\tMonthly Payment of \$" + decimalPlaces.format(monthlyPayment));
System.out.println("\n\n");

System.out.println("\n\n\nMonths\t\tPrincipal\tInterest\tBalance");
System.out.println("Remaining\tPayment\t\tPayment\t\tRemaining");
System.out.println("---------\t---------\t---------\t---------");

/// start while loop
while (termMonths > 0)
{
// calculate interest and principal payments

monthlyInterestPayment = (balance * interestRateMonths);
monthlyPrincipalPayment = (monthlyPayment - monthlyInterestPayment);
balance = (balance - monthlyPrincipalPayment);

// format column headers for results to be displayed
System.out.println(termMonths + "\t\t\$" + decimalPlaces.format(monthlyPrincipalPayment) +
"\t\$" + decimalPlaces.format(monthlyInterestPayment) +
"\t\t\$" + decimalPlaces.format(balance));

// decrement months
termMonths--;

// these conditional statements cause the results to pause
if(linecount == 50)
{
linecount = 0;
try
{
Thread.sleep(3000);      // pause to last three seconds

// formating used to set column headers for results
System.out.println("\n\n\nMonths\t\tPrincipal\tInterest\tBalance");
System.out.println("Remaining\tPayment\t\tPayment\t\tRemaining");
System.out.println("---------\t---------\t---------\t---------");
}

catch (InterruptedException e)
{
}
}      // end if
else
{
linecount++;
}
// end else
}// end while
}              // end main
}
}// end MtgCalc
0
Question by:antcal

LVL 92

Accepted Solution

objects earned 2000 total points
ID: 16583231
>                balance = principal;               // set current balance = principal

move that line inside your first loop
0

LVL 9

Expert Comment

ID: 16583421
>>Double interestRateYears[]={5.35, 5.5, 5.75};

this should be

double interestRateYears[]={5.35, 5.5, 5.75};
0

Author Comment

ID: 16584741
Thanks ALOT objects! It finally works!
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
###### Suggested Courses
Course of the Month13 days, 13 hours left to enroll