Member Avatar for buchanan23

The code I pasted is for a school assignment where we have to calculate the monthly payment for 3 different loans, with varying interest rates, and then display some information about each payment for each of the 3 loans.

My code compiles and runs fine (not finished commenting all the sections yet), but my question is how come the balance is $-0.00 at the end of the first loan, but not on the other two loans?

Any help is greatly appreciated.

Thanks.
Brandon

public class MonthlyPaymentWk5 {
  public static void main(String[] args) {
  
    // Declare Variables to be used
    double principle = 200000.00;     		// The total balance owed is $200,000.00
    double[] interest = {5.35, 5.5, 5.75};  // Create the interest array for the 3 different interest percentages
    int[] term = {7, 15, 30};               // Create the term array for the 3 different terms
    double numMonths = 0;					// Variable to hold the # of months for each term
    double monthlyInterest = 0;				// Variable to hold the monthly interest for each term
    double monthlyPayment= 0;				// Variable to hold the monthly payment for each term
	double principlePayment = 0;	 		// Variable to hold portion of payment made to principle
	double interestPayment = 0;		 		// Variable to hold portion of payment made to interest
	double balance = 0;				 		// Variable to hold current loan balance

	java.text.DecimalFormat dcm= new java.text.DecimalFormat("$,###.00"); // Formats dollar amount outputs to look like money



    /* Determine the interest per month, and how many total months there are for each term
	   using the arrays that were previously declared for term and interest */
		int i;
		for (i=0; i<term.length; i++) {

		monthlyInterest = (interest[i] / (12 * 100));
    	numMonths = (term[i] * 12);

    
   		//Calculate the amortized monthly payment for each loan
    	monthlyPayment = principle * ( monthlyInterest / (1 - Math.pow(1 + monthlyInterest, -numMonths)));
    
    	// Output the monthly payment amount for each loan
    	System.out.println(); // Print a blank line for a break in the visual output
		System.out.println("Loan " + (i+1) + ":");
    	System.out.println("A " + dcm.format(principle) + " loan with an interest rate of " + interest[i] + "% over a period of " + term[i] + " years"); // Print the first line
    	System.out.println("has a monthly payment of " + dcm.format(monthlyPayment));  // Print the second line, rounding to two decimal places and 1 new line commands

		balance = principle;
		interestPayment = balance * (interest[i] / (12 * 100));
		principlePayment = monthlyPayment - interestPayment;
		
		System.out.println("Loan " + (i+1) + ":");
		System.out.println("The interest and balance amounts for each payment are as follows:");

		int j;
		for (j=0; j < numMonths; j++){

		System.out.printf("Payment " + (j+1) + ": Interest Paid - $%.2f", + interestPayment );
		System.out.printf("    Remaining Balance - $%.2f\n", + (balance - principlePayment));

		// These 3 lines make the necessary subtractions so that the loop does not repeat on the same numbers over and over
		balance = balance - principlePayment;
		interestPayment = balance * (interest[i] / (12 * 100));
		principlePayment = monthlyPayment - interestPayment;
		
		// This section is to display 20 lines at a time and then to pause for a few moments before continuing on 
		if (j % 20 == 0 && j != 0) {
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) { }
			System.out.println();
			}

		}
		}
	System.out.println(); // Print a blank line for a break in the visual output
  }
}

Recommended Answers

All 8 Replies

Member Avatar for hfx642

What if the outstanding balance is actually $-0.003,
but since you are only displaying 2 decimal places, you see $-0.00

Member Avatar for buchanan23

Good point. I guess then what I am trying to accomplish is to bring it down to a positive zero balance like the other two loans. So maybe I need to put something in place that doesn't allow it to go negative? Is that possible?

Member Avatar for hfx642

Anything is possible. I all depends on how you code it.

Member Avatar for buchanan23

I figured that much. Well, hopefully someone will reply with some help on this one.

Thanks.

Round the results of your calculations to 2 decimal places (cents) as you perform them, or just hold and calculate all monetary amounts in integer cents. Loans are not normally calculated in fractions of a cent.

Member Avatar for buchanan23

Thanks, I was under the impression that this code

$%.2f

was what did the rounding to two decimal places... is this not the case? Do you think you could give me an example of what it should look like if that code is not rounding?

Thanks.

To round the results of your calculations to 2 decimal places you can use
d = Math.round(amount*100)/100.0d;
Personally I would always use an integer type and hold amounts in cents, but that's just me.

Member Avatar for buchanan23

Thanks for the help, I will see if I can figure out how to implement that into my current code, not sure where to start so it will probably take me some time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.