# Thread: Calculate the remaining balance on a loan

1. Member Join Date
Jan 2010
Posts
80
Rep Power
0

## 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

Java 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.  Reply With Quote

2. ## 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)?  Reply With Quote

3. Senior Member Join Date
Mar 2010
Posts
266
Rep Power
10

## 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
Java Code:
`System.out.print(balance[l]);`
with
Java Code:
`System.out.print(balance[(j-1)*numRate + l]);`  Reply With Quote

4. Member Join Date
Jan 2010
Posts
80
Rep Power
0

##  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  Reply With Quote

5. Member Join Date
Jan 2010
Posts
80
Rep Power
0

##  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
Java Code:
`System.out.print(balance[l]);`
with
Java Code:
`System.out.print(balance[(j-1)*numRate + l]);`
Sorry, it doesn't work
:(  Reply With Quote

6. ##  Originally Posted by ŖàΫ ỏƒ Ңόρę I have to use 1-dimensional
So currently you have something like this (please correct me if I'm misunderstanding you):
Java 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:

Java 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```  Reply With Quote

7. Senior Member Join Date
Mar 2010
Posts
952
Rep Power
10

## 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-  Reply With Quote

8. ## He's not even using a class here but a single main method.  Reply With Quote

9. Member Join Date
Jan 2010
Posts
80
Rep Power
0

##  Originally Posted by Fubarable So currently you have something like this (please correct me if I'm misunderstanding you):
Java 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:

Java 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

Java 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```  Reply With Quote

10. Member Join Date
Jan 2010
Posts
80
Rep Power
0

##  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  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•