# Calculate the remaining balance on a loan

• 03-30-2010, 02:08 AM
ŖàΫ ỏƒ Ңόρę
Calculate the remaining balance on a loan
Hi

I have to write a programe that calculate the remaining balance on a loan for a series of monthly payments and a series of interest rates.

I have to use an array to store the balance for each interest rate and using nested for loops to print the table

the problem is when I enter the values, the programe prints the same value in each balance

Code:

```import java.util.Scanner; import java.text.NumberFormat; public class Loan {         public static void main(String[] args){                                 Scanner scan = new Scanner(System.in);                 NumberFormat formatter = NumberFormat.getCurrencyInstance();                                 System.out.print("Enter amount of loan: ");                 double amount = scan.nextDouble();                                 System.out.print("Enter lowest interest rate: ");                 int lowest = scan.nextInt();                                 System.out.print("Enter monthly payment: ");                 double monthly = scan.nextDouble();                                 System.out.print("Enter number of payments: ");                 int numPay = scan.nextInt();                                 System.out.print("Enter number of interest rates: ");                 int numRate = scan.nextInt();                                 double[] balance = new double[numRate*numPay];                                 System.out.print("payment #");                 for(int i=1; i<=numRate; i++){                         System.out.print("\t");                         System.out.print("balance "+formatter.format(lowest));                         for(int k=0; k<balance.length;k++){                                                                 amount = amount + (amount*((lowest/100)/12));                                 balance[k]= amount-monthly;                                                         }                         lowest =lowest +1;                                         }                 System.out.print("\n");                 for(int j=1; j<=numPay; j++){                         System.out.print(j);                         for(int l=0; l<numRate ;l++){                         System.out.print("\t");                         System.out.print(balance[l]);                         }                         System.out.print("\n");                 }         } }```
* To find the monthly interest rate, convert the interest rate provided by the user to a percentage and divide it by 12.
• 03-30-2010, 02:23 AM
Fubarable
Are you supposed to use a 1-dimensional array, or should it be a 2-dimensional array (which if I understand you correctly seems to make more sense to me)?
• 03-30-2010, 02:28 AM
iluxa
looking at your printing double loop, what you're effectively doing is iterating over the first numRate elements of the array, and you're doing that numPay times.

Probably not what you want.

Replace
Code:

`System.out.print(balance[l]);`
with
Code:

`System.out.print(balance[(j-1)*numRate + l]);`
• 03-30-2010, 02:47 AM
ŖàΫ ỏƒ Ңόρę
Quote:

Originally Posted by Fubarable
Are you supposed to use a 1-dimensional array, or should it be a 2-dimensional array (which if I understand you correctly seems to make more sense to me)?

I have to use 1-dimensional

Thank u
• 03-30-2010, 02:48 AM
ŖàΫ ỏƒ Ңόρę
Quote:

Originally Posted by iluxa
looking at your printing double loop, what you're effectively doing is iterating over the first numRate elements of the array, and you're doing that numPay times.

Probably not what you want.

Replace
Code:

`System.out.print(balance[l]);`
with
Code:

`System.out.print(balance[(j-1)*numRate + l]);`

Sorry, it doesn't work
:(
• 03-30-2010, 02:53 AM
Fubarable
Quote:

Originally Posted by ŖàΫ ỏƒ Ңόρę
I have to use 1-dimensional

So currently you have something like this (please correct me if I'm misunderstanding you):
Code:

```outer loop -- change the interest rate   inner loop one -- calculate the balance and place in array end outer loop outer loop -- change the interest rate   inner loop two -- print out the balance array end outer loop```
But when you do the printing portion, the only thing remaining in the balance array are the calculations from the last iteration of the outer loop above.

Instead, what if you do the calculations and printing both within the outer loop:

Code:

```outer loop -- change the interest rate   inner loop one -- calculate the balance and place in array   inner loop two -- print out the balance array end outer loop```
• 03-30-2010, 03:28 AM
gcalvin
Are you required to do this within one class called Loan? If so, shame on your teacher for twisting your thinking. To my mind, one loan has one principal amount and one interest rate. A table like you're describing would be created by generating a collection of Loans. If you are required to do this all in one class, can you at least name that class AmortizationTable (or even better, AmortizationTablePrinter) instead of Loan? So much of programming -- especially Java programming -- is about developing habits and patterns of clear thinking. Calling something a Loan when it is really a table of possible loans (or more precisely, a program to generate a table of possible loans -- they're not the same thing) just seems to work against clear thinking.

-Gary-
• 03-30-2010, 03:32 AM
Fubarable
He's not even using a class here but a single main method.
• 03-30-2010, 03:37 AM
ŖàΫ ỏƒ Ңόρę
Quote:

Originally Posted by Fubarable
So currently you have something like this (please correct me if I'm misunderstanding you):
Code:

```outer loop -- change the interest rate   inner loop one -- calculate the balance and place in array end outer loop outer loop -- change the interest rate   inner loop two -- print out the balance array end outer loop```
But when you do the printing portion, the only thing remaining in the balance array are the calculations from the last iteration of the outer loop above.

Instead, what if you do the calculations and printing both within the outer loop:

Code:

```outer loop -- change the interest rate   inner loop one -- calculate the balance and place in array   inner loop two -- print out the balance array end outer loop```

I think this one is better

Code:

```outer loop -- change the interest rate   inner loop one -- calculate the balance and place in array   inner loop two -- print out the balance array end outer loop```
• 03-30-2010, 03:38 AM
ŖàΫ ỏƒ Ңόρę
Quote:

Originally Posted by gcalvin
Are you required to do this within one class called Loan? If so, shame on your teacher for twisting your thinking. To my mind, one loan has one principal amount and one interest rate. A table like you're describing would be created by generating a collection of Loans. If you are required to do this all in one class, can you at least name that class AmortizationTable (or even better, AmortizationTablePrinter) instead of Loan? So much of programming -- especially Java programming -- is about developing habits and patterns of clear thinking. Calling something a Loan when it is really a table of possible loans (or more precisely, a program to generate a table of possible loans -- they're not the same thing) just seems to work against clear thinking.

-Gary-

yup I have to use it within one class

It's very hard actually