I need help with this Java program for school. I have to add loops so that each loan will loop and show the payment made, the int payment made, and the loan balance for each month of the entire loan. There are three different loans, 7 year, 15 year, and 30 year. Each with a different interest rate. I have that part done, just need to figure out how to put the loops in to show each months payment and ending loan balance. I've tried do/while, for, and if/else, and I can't get any of them to work. Can someone please help me?

import java.io.*;
import java.util.Arrays;

public class MortgageCalculator
{
	public static void main(String[] args) throws Exception
	{
	//Assign variables for principal amount
		double principal = 200000;					//Principal amount of the loan is $200,000
		double PrinAmt1, PrinAmt2, PrinAmt3;		//Assign 3 different principal amounts
		double Payment1, Payment2, Payment3;		//Assign 3 different payment amounts
		double Int1, Int2, Int3;					//Assign 3 different interest rate amounts
		double moPrin1, moPrin2, moPrin3;			//Assign 3 different monthly principal payment amounts
		double LoanBal = 0;
		PrinAmt1 = principal;
		PrinAmt2 = principal;
		PrinAmt3 = principal;
		moPrin1 = 0;
		moPrin2 = 0;
		moPrin3 = 0;
		Int1 = 0;
		Int2 = 0;
		Int3 = 0;
		int IntCounter = 0;
		
		//Term of Loan Array
		
		int [] Term = {84, 180, 360};			//Loan years * 12
		
		//Interest Rate Array
		
		double [] Rate = {0.0535, 0.055, 0.0575};
		
		//Loan Amount Array
		
		double [] LoanAmt = {200000, 200000, 200000};
		
		//Each payment calculation
		
		Payment1 = (principal * Rate [0] / 12)/(1-1/Math.pow((1+Rate[0]/12), Term[0]));
		Payment2 = (principal * Rate [1] / 12)/(1-1/Math.pow((1+Rate[1]/12), Term[1]));
		Payment3 = (principal * Rate [2])/ 12/(1-1/Math.pow((1+Rate[2]/12), Term[2]));
		
		//Print to screen Loan - Amount, Years, Interest rate and monthly payment for a 7-year loan.
		
		
	do{
		IntCounter = IntCounter ++;			//Add one to payment # for each month
		Int1 = PrinAmt1 = Payment1 * Int1;	//Monthly Interest payment
		Payment1 = Payment1 - Int1;			//Principal payment
		PrinAmt1 = PrinAmt1 - Payment1;		//Principal minus one payment
		
		System.out.println("Loan Amount: $200,000.00");
		System.out.println("Term: 7 Years");
		System.out.println("Interest Rate 5.35%");
		System.out.printf("Monthly payment amount is $%.2f\n", Payment1);
	
		
		
	}while (PrinAmt1 >= 0);
	
	do{
		IntCounter = IntCounter ++;			//Add one to payment # for each month
		Int2 = PrinAmt2 = Payment2* Int2;	//Monthly Interest payment
		Payment2 = Payment2 - Int2;			//Principal payment
		PrinAmt2 = PrinAmt2 - Payment2;		//Principal minus one payment
			
		System.out.println("\nLoan Amount: $200,000.00");
		System.out.println("Term: 15 Years");
		System.out.println("Interest Rate 5.5%");
		System.out.printf("The monthly payment amount is $%.2f\n", Payment2);
		System.out.println( );
		
	}while (PrinAmt2 >= 0);
		
		do{
			IntCounter = IntCounter ++;			//Add one to payment # for each month
			Int3 = PrinAmt3 = Payment3 * Int3;	//Monthly Interest payment
			Payment3 = Payment3- Int3;			//Principal payment
			PrinAmt3 = PrinAmt3 - Payment3;		//Principal minus one payment
			
			System.out.println("Loan Amount: $200,000.00");
			System.out.println("Term: 30 Years");
			System.out.println("Interest Rate 5.75%");
			System.out.printf("Monthly payment amount is $%.2f\n", Payment3);
			System.out.println( );
		}while (PrinAmt3 >= 0);
		
			
					
				}
			
		
		
}

Recommended Answers

All 7 Replies

Can you show the output you now have, describe what is wrong with it and show what you'd like it to be?

Below is the output I want, but in addition, I have to put in loops which show each payment, interest paid and loan balance after each payment is made for each of the loans. I cannot get my loops to work, I keep getting errors when I'm trying to construct them.

Loan Amount: $200,000.00
Term: 7 Years
Interest Rate 5.35%
Monthly payment amount is $2859.79
Payment # 1

Loan Amount: $200,000.00
Term: 15 Years
Interest Rate 5.5%
The monthly payment amount is $1634.17

Loan Amount: $200,000.00
Term: 30 Years
Interest Rate 5.75%
Monthly payment amount is $1167.15

This is what it looks like now, in addition I need it to do the following:

Loan Amount: $200,000.00
Term: 7 Years
Interest Rate 5.35%
Monthly payment amount is $2859.79

This is what I need it to do:
Payment # 1
Payment Received 2859.79
Interest Payment Received ?not sure what this is supposed to be
Loan Balance $whatever the loan balance is after payment is received.

And I need it to do this under each of one of the different loan amounts, rates and years.

Loan Amount: $200,000.00
Term: 15 Years
Interest Rate 5.5%
The monthly payment amount is $1634.17

Loan Amount: $200,000.00
Term: 30 Years
Interest Rate 5.75%
Monthly payment amount is $1167.15

I keep getting errors when I'm trying to construct them.

please copy and paste the full text of the error message here.

Interest Payment Received ?not sure what this is supposed to be
Loan Balance $whatever the loan balance is after payment is received.

Is that a question of what formula you need to compute the result?

Google should give you the forumulas you need to compute the values.

I guess the problem is not clear to you as well as to the viewer of the site.
Please paste the exact assignment or the inputs and the output you want...

I need help with this Java program for school. I have to add loops so that each loan will loop and show the payment made, the int payment made, and the loan balance for each month of the entire loan. There are three different loans, 7 year, 15 year, and 30 year. Each with a different interest rate. I have that part done, just need to figure out how to put the loops in to show each months payment and ending loan balance. I've tried do/while, for, and if/else, and I can't get any of them to work. Can someone please help me?

import java.io.*;
import java.util.Arrays;

public class MortgageCalculator
{
	public static void main(String[] args) throws Exception
	{
	//Assign variables for principal amount
		double principal = 200000;					//Principal amount of the loan is $200,000
		double PrinAmt1, PrinAmt2, PrinAmt3;		//Assign 3 different principal amounts
		double Payment1, Payment2, Payment3;		//Assign 3 different payment amounts
		double Int1, Int2, Int3;					//Assign 3 different interest rate amounts
		double moPrin1, moPrin2, moPrin3;			//Assign 3 different monthly principal payment amounts
		double LoanBal = 0;
		PrinAmt1 = principal;
		PrinAmt2 = principal;
		PrinAmt3 = principal;
		moPrin1 = 0;
		moPrin2 = 0;
		moPrin3 = 0;
		Int1 = 0;
		Int2 = 0;
		Int3 = 0;
		int IntCounter = 0;
		
		//Term of Loan Array
		
		int [] Term = {84, 180, 360};			//Loan years * 12
		
		//Interest Rate Array
		
		double [] Rate = {0.0535, 0.055, 0.0575};
		
		//Loan Amount Array
		
		double [] LoanAmt = {200000, 200000, 200000};
		
		//Each payment calculation
		
		Payment1 = (principal * Rate [0] / 12)/(1-1/Math.pow((1+Rate[0]/12), Term[0]));
		Payment2 = (principal * Rate [1] / 12)/(1-1/Math.pow((1+Rate[1]/12), Term[1]));
		Payment3 = (principal * Rate [2])/ 12/(1-1/Math.pow((1+Rate[2]/12), Term[2]));
		
		//Print to screen Loan - Amount, Years, Interest rate and monthly payment for a 7-year loan.
		
		
	do{
		IntCounter = IntCounter ++;			//Add one to payment # for each month
		Int1 = PrinAmt1 = Payment1 * Int1;	//Monthly Interest payment
		Payment1 = Payment1 - Int1;			//Principal payment
		PrinAmt1 = PrinAmt1 - Payment1;		//Principal minus one payment
		
		System.out.println("Loan Amount: $200,000.00");
		System.out.println("Term: 7 Years");
		System.out.println("Interest Rate 5.35%");
		System.out.printf("Monthly payment amount is $%.2f\n", Payment1);
	
		
		
	}while (PrinAmt1 >= 0);
	
	do{
		IntCounter = IntCounter ++;			//Add one to payment # for each month
		Int2 = PrinAmt2 = Payment2* Int2;	//Monthly Interest payment
		Payment2 = Payment2 - Int2;			//Principal payment
		PrinAmt2 = PrinAmt2 - Payment2;		//Principal minus one payment
			
		System.out.println("\nLoan Amount: $200,000.00");
		System.out.println("Term: 15 Years");
		System.out.println("Interest Rate 5.5%");
		System.out.printf("The monthly payment amount is $%.2f\n", Payment2);
		System.out.println( );
		
	}while (PrinAmt2 >= 0);
		
		do{
			IntCounter = IntCounter ++;			//Add one to payment # for each month
			Int3 = PrinAmt3 = Payment3 * Int3;	//Monthly Interest payment
			Payment3 = Payment3- Int3;			//Principal payment
			PrinAmt3 = PrinAmt3 - Payment3;		//Principal minus one payment
			
			System.out.println("Loan Amount: $200,000.00");
			System.out.println("Term: 30 Years");
			System.out.println("Interest Rate 5.75%");
			System.out.printf("Monthly payment amount is $%.2f\n", Payment3);
			System.out.println( );
		}while (PrinAmt3 >= 0);
		
			
					
				}
			
		
		
}

Now I am working on getting the GUI into the Mortgage Calculator. The Mortgage Calculator does not like the fields I'm using in the GUI. It also does not like the -

class MortgageCalculator implements ActionListener, Runnable{

Can you tell me what I'm not seeing, please?

Here is my code - the GUI code first, and then the Mortgage Calculator code.

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.*;
import javax.swing.*;



public class BeemanGUI extends JFrame{
	
	public BeemanGUI() {
		
		//Set up Window Panel
		super("Beeman's Mortgage Calculator");
		setSize(550, 270);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
		JPanel pane = new JPanel();
		BoxLayout box = new BoxLayout(pane, BoxLayout. Y_AXIS);
		pane.setLayout(box);
		FlowLayout flo = new FlowLayout();
		setLayout(flo);
		
		//Set up Labels and Buttons for Window Panel
		JLabel pageLabel = new JLabel ("Loan Amount: ", JLabel.RIGHT);
		JTextField LoanAmount = new JTextField(10);
		add(pageLabel);
		add(LoanAmount); 
		JButton InterestRate1 = new JButton ("7 Years @ 5.35%");
		add(InterestRate1);
		JButton InterestRate2 = new JButton ("15 Years @ 5.5%");
		add(InterestRate2);
		JButton InterestRate3 = new JButton ("35 years @ 5.75%");
		add(InterestRate3);
		JLabel pageLabel2 = new JLabel ("Monthly Payment");
		JTextField MonthlyPayment = new JTextField(10);
		add(pageLabel2);
		add(MonthlyPayment);
		JButton Reset = new JButton ("Reset");
		add(Reset);
		pane.add(InterestRate1);
		pane.add(InterestRate2);
		pane.add(InterestRate3);
		pane.add(Reset);
		add(pane);
	}
	public static void main (String[] arguments){
		BeemanGUI window = new BeemanGUI();
	}
	}

This is the Mortgage Calculator that I'm trying to get the GUI to run in.

//Prg/421
					//Debora Beeman
					//Henry Williams - Instructor
					//Mortgage Calculator
					//Due Week 2 - Day 7

import static java.lang.Math.*;
import java.text.DecimalFormat;
import java.io.*;
import java.util.Arrays;
import javax.swing.*;
import java.awt.event.*;


class MortgageCalculator implements ActionListener, 
	Runnable{
	
	BeemanGUI gui;
	Thread calculating;
	
	public MortgageCalculator(BeemanGUI in) {
		gui = in;
	}
	public void actionPerformed(ActionEvent event) {
		String command = event.getActionCommand();
		if(command.equals("7 Years @ 5.35%"))
		{
			startCalculating();
		}
		if(command.equals("15 Years @ 5.5%"))
		{
			startCalculating();
		}
		if(command.equals("30 Years @ 5.75%"))
		{
			startCalculating();
		}
		if(command.equals("Reset"))
		{
			clearAllFields();}
	}
		private void clearAllFields() {
		// TODO Auto-generated method stub
		
	}
		void startCalculating(){
			calculating = new Thread(this);
			calculating.start();
			gui.InterestRate1.setEnabled(false);
			gui.InterestRate2.setEnabled(false);
			gui.InterestRate3.setEnabled(false);
			gui.Reset.setEnabled(false);
			gui.MonthlyPayment.setEnabled(false);
		}
		void stopCalculating(){
			gui.interestRate1.setEnabled(true);
			gui.interestRate2.setEnabled(true);
			gui.interestRate3.setEnabled(true);
			gui.Reset.setEnabled(true);
			gui.MonthlyPayment.setEnabled(true);
			calculating = null;
		
		
	}
    public static void main(String[] args) {
        double Principal = 200000;              //Set Variable for Loan
        double Int [] = {.0535, .055, .0575};   //Array for Interest Amounts
        double Year [] = {84, 180, 360};        //Array for Terms
        double MonthlyInterest [] = { (Int[0] / 12) , (Int[1] / 12) , (Int[2] / 12)};  //Array for Monthly Interest
        double LastMonthPay [] = {0,0,0};       //Array for Previous Month Payment
        double IntRatePd1;                      //Set Variable
        double IntRatePd2;                      //Set Variable
        double IntRatePd3;                      //Set Variable
        double Payment1;                        //Set Variable
        double Payment2;                        //Set Variable
        double Payment3;                        //Set Variable
        int IntCounter = 1;                 	//Counter for each payment
        new DecimalFormat("$0.00");
    
        double Loan[]; //Calculating 3 different loans
        Loan = new double[3];
        Loan[0]=  (((Principal * Int[0])* Year[0]) + Principal);
        Loan[1]=  (((Principal * Int[1])* Year[1]) + Principal);
        Loan[2]=  (((Principal * Int[2])* Year[2]) + Principal);

  
        double RemBal[] = { (Loan[0]), (Loan[1]), (Loan[2])};   //Remaining Balance for each loan

    
        while (IntCounter > 0) {    //Payment # Counter for up to 30-year loan

        	//Set the speed the results will scroll
        	try
        	{
        		Thread.sleep(800);
        	} catch (InterruptedException e) { }

        
        Payment1 = (Principal * Int [0] / 12)/(1-1/Math.pow((1+Int[0]/ 12), Year[0])); //Calculate Monthly Payments for 7-year loan
        Payment2 = (Principal * Int [1] / 12)/(1-1/Math.pow((1+Int[1]/ 12), Year[1])); //Calculate Monthly Payments for 15-year loan
        Payment3 = (Principal * Int [2] / 12)/(1-1/Math.pow((1+Int[2]/ 12), Year[2])); //Calculate Monthly Payments for 30-year loan
        
        double CurrentBalance[];	//Set Current Balance for all loans
        CurrentBalance= new double[3];
        CurrentBalance[0] = (RemBal[0] - Payment1);
        CurrentBalance[1] = (RemBal[1] - Payment2);
        CurrentBalance[2] = (RemBal[2] - Payment3);

        IntRatePd1 = ((MonthlyInterest[0] * (CurrentBalance[0] - LastMonthPay[0]))/10); //Monthly Interest paid for 7-year loan
        IntRatePd2 = ((MonthlyInterest[1] * (CurrentBalance[1] - LastMonthPay[1]))/10); //Monthly Interest paid for 15-year loan
        IntRatePd3 = ((MonthlyInterest[2] * (CurrentBalance[2] - LastMonthPay[2]))/10); //Monthly Interest paid for 30-year loan
        
     
        LastMonthPay[0] = Payment1; //Previous month payment
        LastMonthPay[1] = Payment2; //Previous month payment
        LastMonthPay[2] = Payment3; //Previous month payment

        IntCounter = (IntCounter + 1);  //Change Payment # Count

        RemBal[0] = (RemBal[0] - Payment1); //Change Remaining Balance
        RemBal[1] = (RemBal[1] - Payment2);
        RemBal[2] = (RemBal[2] - Payment3);

    // Output of Monthly Payments, Interest, Remaining Balance
        System.out.println("Payment # " + IntCounter);
        System.out.print("Payment Received $ " +  Payment1 + "  at  " + Int[0] + " % Rate: \n");
        System.out.print("Remaining Balance = $ " + RemBal[0] + (CurrentBalance[0]));
        System.out.println("Monthly Interest Paid = $ \n" + IntRatePd1);
        
        System.out.print("Payment Received $ " +  Payment2 + "  at  " + Int[1] + "  % Rate: \n");
        System.out.print("Remaining Balance = $ " + RemBal[1] + (CurrentBalance[1]));
        System.out.println("Monthly Interest Paid = $ \n" + IntRatePd2);
        
        System.out.print("Payment Received $ " +  Payment3 + "  at  " + Int[2] + " % Rate: \n");
        System.out.print("Remaining Balance = $ " + RemBal[2] + (CurrentBalance[2]));
        System.out.println("Monthly Interest Paid = $ \n" + IntRatePd3);

    
    }
}
}
Member Avatar for coil

What's the actual error?

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.