Results 1 to 10 of 10
  1. #1
    Join Date
    Jan 2010
    Posts
    80
    Rep Power
    0

    Default 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.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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)?

  3. #3
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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]);

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

    Default

    Quote Originally Posted by Fubarable View Post
    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

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

    Default

    Quote Originally Posted by iluxa View Post
    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
    :(

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by ŖΫ ỏƒ Ңόρę View Post
    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

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    He's not even using a class here but a single main method.

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

    Default

    Quote Originally Posted by Fubarable View Post
    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

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

    Default

    Quote Originally Posted by gcalvin View Post
    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

Similar Threads

  1. Getting the remaining cents
    By MyOnlineChurva in forum New To Java
    Replies: 5
    Last Post: 01-01-2010, 02:37 AM
  2. Color Balance
    By THEAniKan in forum Java 2D
    Replies: 2
    Last Post: 09-14-2009, 04:34 PM
  3. Binary Tree Deletion & Balance Methods
    By sev51 in forum New To Java
    Replies: 1
    Last Post: 01-27-2009, 04:26 PM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Formula on Maximum loan..
    By esh21 in forum NetBeans
    Replies: 4
    Last Post: 08-14-2008, 02:27 PM

Posting Permissions

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