The question is "Where am I going wrong?" See my embedded comments above code starting point. I can get this to compile, but I can't get this loop through the amort, before I beign building (finishing the loop) I need to see if I can display a single line with the folliwng four outputs (interest, monthlypayment, balance)--where am I messing up?

Please help thanks!

Code/
//Need to Create a Mortgage calculator with Amortization display
//The following code compiles, but I can only display one output to the TextArea
//NOt sure how to proceed to get it to display all four pieces:
//(interest, principal, monthly payment, balance) and then loop that till zero?



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;
import javax.swing.event.*;
import java.text.*;
import java.util.*;
import javax.swing.border.*;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.lang.*;
import javax.swing.border.*;
import javax.swing.JOptionPane;
import java.lang.Math.*;
import java.text.NumberFormat;
// Create MortCalc class
public class MortCalc_3 extends JFrame implements ActionListener
{
// Radio Button Selection
JPanel rowOne = new JPanel();
JRadioButton rbOne = new JRadioButton("7 Years/5.35%", true);
JRadioButton rbTwo = new JRadioButton("15 Years/5.5%", true);
JRadioButton rbThree = new JRadioButton("30 Years/5.75%", true);
// Text Amount for Mortgage
JPanel rowTwo = new JPanel();
JLabel mortgageLabel = new JLabel("Mortgage Amount: $", JLabel.LEFT);
JTextField mortgageText = new JTextField(10);
JLabel termLabel = new JLabel("Term (Years):", JLabel.LEFT);
JTextField termText = new JTextField(3);
JLabel intRateLabel = new JLabel("Interest Rate (%):", JLabel.LEFT);
JTextField intRateText = new JTextField(5);
JLabel mPaymentLabel = new JLabel("Your Monthly Payment is: $", JLabel.LEFT);
JTextField mPaymentText = new JTextField(10);
// Create buttons to calculate payment, amortizaton, and clear fields
JPanel rowThree = new JPanel();
JButton calculateButton = new JButton("CALCULATE");
JButton clearButton = new JButton ("START OVER");
JButton exitButton = new JButton("EXIT");
//*** Begin RLW *** ----------------------------------------------
// Create Label for Amort Table
JPanel rowFour = new JPanel();
JLabel paymentLabel = new JLabel("Payment #");
JLabel balLabel = new JLabel(" Balance");
JLabel ytdPrincLabel = new JLabel(" Principal");
JLabel ytdIntLabel = new JLabel(" Interest");
// Create Detail Line for Amort Table
JPanel rowFive = new JPanel();
JTextArea mDisplayTextArea = new JTextArea (10,30);
JScrollPane scroll = new JScrollPane(mDisplayTextArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);


// End RLW *** --------------------------------------------------


public MortCalc_3()
{
super ("*-*Adrienne's Calculator*-*");
setSize(400, 600);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = getContentPane();
JScrollPane scroll1= new JScrollPane(mDisplayTextArea,
JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);


FlowLayout layout = new FlowLayout(FlowLayout.LEFT);
pane.setLayout(layout);
//Add to the screen
FlowLayout layout1 = new FlowLayout();
rowOne.setLayout(layout1);
// Add Buttons
//Borrowed this design from a GUI interface I had seen in Murach's book as well.
//I changed the names and functions to suit this assignment.
ButtonGroup btnGrp = new ButtonGroup();
btnGrp.add(rbOne);
btnGrp.add(rbTwo);
btnGrp.add(rbThree);
rowOne.add(rbOne);
rowOne.add(rbTwo);
rowOne.add(rbThree);
pane.add(rowOne);
// Create a border around the radio buttons
Border titledRadioBorder = BorderFactory.createTitledBorder("Choose Term");
rowOne.setBorder(titledRadioBorder);
// Listeners
rbOne.addActionListener(this);
rbTwo.addActionListener(this);
rbThree.addActionListener(this);
// Add Labels
//Modified from Murach's FutureApp Code (his code was designed for another use--I liked the layout and borrowed the style.//
GridLayout layout2 = new GridLayout(4, 1);
rowTwo.setLayout(layout2);
rowTwo.add(mortgageLabel);
rowTwo.add(mortgageText);
mortgageText.setText("200000");
rowTwo.add(termLabel);
rowTwo.add(termText);
termText.setText("30");
rowTwo.add(intRateLabel);
rowTwo.add(intRateText);
intRateText.setText("5.75");
rowTwo.add(mPaymentLabel);
rowTwo.add(mPaymentText);
mPaymentText.setEnabled(false);
pane.add(rowTwo);
// Add Buttons
FlowLayout layout3 = new FlowLayout(FlowLayout.CENTER,10, 10);
rowThree.setLayout(layout3);
rowThree.add(calculateButton);
calculateButton.setBackground(Color.green);
rowThree.add(clearButton);
clearButton.setBackground(Color.white);
rowThree.add(exitButton);
exitButton.setBackground(Color.red);
pane.add(rowThree);
// Listeners
calculateButton.addActionListener(this);
clearButton.addActionListener(this);
exitButton.addActionListener(this);
// Create a border around the Table
Border titledAmortTable = BorderFactory.createTitledBorder("Amortization Table");
rowFour.setBorder(titledAmortTable);
//*** Begin RLW *** ----------------------------------------------
// Add Amort Table Column Headers (ROW4)
FlowLayout layout4 = new FlowLayout(FlowLayout.CENTER, 10, 10);
rowFour.setLayout(layout4);
rowFour.add(paymentLabel);
rowFour.add(balLabel);
rowFour.add(ytdPrincLabel);
rowFour.add(ytdIntLabel);
pane.add(rowFour);


FlowLayout layout5 = new FlowLayout(FlowLayout.CENTER, 10, 10);
rowFive.setLayout(layout5);
rowFive.add(mDisplayTextArea);
mDisplayTextArea.setEnabled(false);
rowFive.add(scroll1);
pane.add(rowFive);
setContentPane(pane);
setVisible(true);


//*** End RLW *** ------------------------------------------------


}
public static void main(String[] args) throws Exception
{
DecimalFormat dollarAmount = new DecimalFormat("#,###.00");
DecimalFormat percentAmount = new DecimalFormat("##.##");
MortCalc_3 frame = new MortCalc_3();
}
//*******************Works DO NOT TOUCH ABOVE THIS LINE*******************************
public void setResultValue() throws ParseException
{
double balance = Double.parseDouble(mortgageText.getText());
double term = Double.parseDouble(termText.getText());
double interest = Double.parseDouble(intRateText.getText()) / 100.;
int numMonths = (int) (term * 12);
double monthlyInterest = interest / 12.0;
double MonthlyPayment = (balance * ( interest/12))/(1-( Math.pow (1/( 1 +(interest/12)), (term*12))));
double principal = 0.0;
double PaymentNumber = (numMonths++);
mPaymentText.setText(Double.toString(MonthlyPayment));



// Loop for amortization list
for (int count = 1; PaymentNumber <= (term * 12) ; count++)
{
// Calculations to determine amortization
balance = Double.parseDouble(mortgageText.getText());
term = Double.parseDouble(termText.getText());
interest = Double.parseDouble(intRateText.getText()) / 100.;
monthlyInterest = interest / 12.0;
MonthlyPayment = (balance * ( interest/12))/(1-( Math.pow (1/( 1 +(interest/12)), (term*12))));
PaymentNumber = (numMonths++);
balance = balance - (MonthlyPayment);
mDisplayTextArea.setText(Double.toString(MonthlyPayment));



}
}



public void actionPerformed(ActionEvent event)  {
Object objectPressed = event.getSource();
double interest = 0;
if (objectPressed == calculateButton) {
try {
setResultValue();
} catch (Exception ex) {
}
} else if (objectPressed == exitButton) {
System.exit(0);
} else if (objectPressed == clearButton) {
termText.setText(null);
intRateText.setText(null);
mPaymentText.setText(null);
}
else if (objectPressed == rbOne || objectPressed ==
rbTwo || objectPressed == rbThree)
{
int arrayIndex = 0;
if (objectPressed == rbOne)
{
arrayIndex = 0;
}
else if (objectPressed == rbTwo)
{
arrayIndex = 1;
}
else if (objectPressed == rbThree)
{
arrayIndex = 2;
}
double[] annualInterests = {5.35, 5.5, 5.75}; //Interest Array
int[] numOfYears = {7, 15, 30}; //Term Array
double annInterest = annualInterests[arrayIndex];
int numYears = numOfYears[arrayIndex];
intRateText.setText(Double.toString (annInterest));
termText.setText(Integer.toString (numYears));
}
}
}

/Code

Edited 3 Years Ago by happygeek: fixed formatting

By the way, I'm new to this kind of thing--forums--so if I screwed up how to post, I'm truly sorry--been up all night trying to get to this point, and now I'm completely stuck.

This article has been dead for over six months. Start a new discussion instead.