I am having some trouble configuring my array to read from a sequential file, and then calling on that to fill an array of interests. I think I have the class set up to read the file, but when I run my program the rates array does not get the information. I think my problem is where I am actuall calling the array index, but I am not sure how to do this. Here is my code:
/*
* MortFrame.java
*
* Created on February 24, 2008, 7:28 PM
*/
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.text.*;
import java.util.*;
public class MortFrame extends javax.swing.JFrame {
/** Creates new form MortFrame */
public MortFrame() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
amountLbl = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
paymentLbl = new javax.swing.JLabel();
display = new javax.swing.JLabel();
amount = new javax.swing.JTextField();
termLbl = new javax.swing.JTextField();
rateLbl = new javax.swing.JTextField();
computePay = new javax.swing.JButton();
clear = new javax.swing.JButton();
exit = new javax.swing.JButton();
jPanel2 = new javax.swing.JPanel();
jScrollPane1 = new javax.swing.JScrollPane();
displayField = new javax.swing.JTextArea();
jPanel3 = new javax.swing.JPanel();
mnuBar = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
mnu7rs = new javax.swing.JMenuItem();
mnu15yrs = new javax.swing.JMenuItem();
mnu30yrs = new javax.swing.JMenuItem();
setDefaultCloseOperation(javax.swing.WindowConstan ts.EXIT_ON_CLOSE);
setTitle("Mortgage Calculator");
jPanel1.setBorder(javax.swing.BorderFactory.create TitledBorder("Loan Details"));
amountLbl.setFont(new java.awt.Font("Tahoma", 1, 12));
amountLbl.setText("Loan Amount: $");
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel2.setText("Term:");
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
jLabel3.setText("Rate");
paymentLbl.setFont(new java.awt.Font("Tahoma", 1, 12));
paymentLbl.setText("Monthly Payment: $");
display.setFont(new java.awt.Font("Tahoma", 0, 12));
amount.setFont(new java.awt.Font("Tahoma", 0, 12));
termLbl.setFont(new java.awt.Font("Tahoma", 0, 12));
rateLbl.setFont(new java.awt.Font("Tahoma", 0, 12));
computePay.setText("Calculate");
computePay.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
computePayActionPerformed(evt);
}
});
clear.setText("Clear");
clear.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearActionPerformed(evt);
}
});
exit.setText("Exit");
exit.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
exitActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
.addComponent(amountLbl, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(paymentLbl, javax.swing.GroupLayout.Alignment.TRAILING))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.LEADING)
.addComponent(termLbl, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
.addComponent(rateLbl, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
.addComponent(display, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
.addComponent(amount, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(computePay)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(clear)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(exit, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap())
);
jPanel1Layout.linkSize(javax.swing.SwingConstants. HORIZONTAL, new java.awt.Component[] {clear, computePay, exit});
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
.addComponent(amountLbl)
.addComponent(amount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(termLbl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel3)
.addComponent(rateLbl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
.addComponent(paymentLbl)
.addComponent(display))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax. swing.GroupLayout.Alignment.BASELINE)
.addComponent(computePay)
.addComponent(clear)
.addComponent(exit))
.addContainerGap(100, Short.MAX_VALUE))
);
jPanel2.setBorder(javax.swing.BorderFactory.create TitledBorder("Amortization Table\n"));
displayField.setColumns(20);
displayField.setRows(5);
jScrollPane1.setViewportView(displayField);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
.addContainerGap())
);
jPanel3.setBorder(javax.swing.BorderFactory.create TitledBorder("Loan Chart\n"));
javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
jPanel3.setLayout(jPanel3Layout);
jPanel3Layout.setHorizontalGroup(
jPanel3Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGap(0, 680, Short.MAX_VALUE)
);
jPanel3Layout.setVerticalGroup(
jPanel3Layout.createParallelGroup(javax.swing.Grou pLayout.Alignment.LEADING)
.addGap(0, 261, Short.MAX_VALUE)
);
jMenu1.setText("Term");
mnu7rs.setText("7yrs @ 5.35%");
mnu7rs.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mnu7rsActionPerformed(evt);
}
});
jMenu1.add(mnu7rs);
mnu15yrs.setText("15yrs @ 5.50%");
mnu15yrs.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mnu15yrsActionPerformed(evt);
}
});
jMenu1.add(mnu15yrs);
mnu30yrs.setText("30yrs @ 5.75%");
mnu30yrs.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mnu30yrsActionPerformed(evt);
}
});
jMenu1.add(mnu30yrs);
mnuBar.add(jMenu1);
setJMenuBar(mnuBar);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.TRAILING)
.addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(javax.swing.GroupLayout.Alignment.LEADIN G, layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_S IZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout .Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.G roupLayout.Alignment.LEADING, false)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.Component Placement.RELATED)
.addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addContainerGap())
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void clearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearActionPerformed
amount.setText("");
display.setText("");
displayField.setText("");
termLbl.setText("");
rateLbl.setText("");
}//GEN-LAST:event_clearActionPerformed
private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
System.exit(1);
}//GEN-LAST:event_exitActionPerformed
private void computePayActionPerformed(java.awt.event.ActionEve nt evt) {//GEN-FIRST:event_computePayActionPerformed
try {
// calculate the monthly payment
double p=Double.parseDouble(amount.getText());
double r=Double.parseDouble(rateLbl.getText())/1200;
double n=Integer.parseInt(termLbl.getText())*12;
double monthlyPayment=p*Math.pow(1+r,n)*r/(Math.pow(1+r,n)-1);
DecimalFormat df = new DecimalFormat("$###,###.00");
display.setText(df.format(monthlyPayment));
// calculate the detailed loan
double principal=p;
int month;
StringBuffer buffer=new StringBuffer();
buffer.append("Month\tAmount\tInterest\tBalance\n" );
for (int i=0; i<n; i++) {
month=i+1;
double interest=principal*r;
double balance=principal+interest-monthlyPayment;
buffer.append(month+"\t");
buffer.append(new String(df.format(principal))+"\t");
buffer.append(new String(df.format(interest))+"\t");
buffer.append(new String(df.format(balance))+"\n");
principal=balance;
}
displayField.setText(buffer.toString());
} catch(Exception ex) {
System.out.println(ex);
}
}//GEN-LAST:event_computePayActionPerformed
public void getRates(){ // START OF NEW CODE
try{
ArrayList<String> InterestRates = new ArrayList<String>();
BufferedReader inputfile
= new BufferedReader(new FileReader("rates.txt"));
String data;
// debugged by hiwa 1 Jun. 2006
while ((data = inputfile.readLine()) != null){
System.out.println(data);
InterestRates.add(data);
}
rates = new double[InterestRates.size()];
for (int x = 0; x < rates.length; ++x){
rates[x] = Double.parseDouble(InterestRates.get(x));
}
inputfile.close();
}
catch(Exception ec)
{
}
}
private void mnu7rsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnu7rsActionPerformed
termLbl.setText("" + iTerms[0]);
rateLbl.setText("" + rates[0]);
}//GEN-LAST:event_mnu7rsActionPerformed
private void mnu15yrsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnu15yrsActionPerformed
termLbl.setText("" + iTerms[1]);
rateLbl.setText("" + rates[x]);
}//GEN-LAST:event_mnu15yrsActionPerformed
private void mnu30yrsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnu30yrsActionPerformed
termLbl.setText("" + iTerms[2]);
rateLbl.setText("" + rates[x]);
}//GEN-LAST:event_mnu30yrsActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new MortFrame().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
int iTerms[] = {7, 15, 30};
double[] rates;
int x;
private javax.swing.JTextField amount;
private javax.swing.JLabel amountLbl;
private javax.swing.JButton clear;
private javax.swing.JButton computePay;
private javax.swing.JLabel display;
private javax.swing.JTextArea displayField;
private javax.swing.JButton exit;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JMenu jMenu1;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel jPanel3;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JMenuItem mnu15yrs;
private javax.swing.JMenuItem mnu30yrs;
private javax.swing.JMenuItem mnu7rs;
private javax.swing.JMenuBar mnuBar;
private javax.swing.JLabel paymentLbl;
private javax.swing.JTextField rateLbl;
private javax.swing.JTextField termLbl;
// End of variables declaration//GEN-END:variables
}