Problem reading a sequential file and creating an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stevedub
    New Member
    • Jan 2007
    • 40

    #1

    Problem reading a sequential file and creating an array

    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.InputSt ream;
    import java.io.FileInp utStream;
    import java.io.IOExcep tion;

    import java.io.*;
    import javax.swing.*;
    import javax.swing.eve nt.*;
    import java.awt.*;
    import java.awt.event. *;
    import java.text.*;
    import java.util.*;

    public class MortFrame extends javax.swing.JFr ame {

    /** 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="c ollapsed" desc="Generated Code">//GEN-BEGIN:initCompo nents
    private void initComponents( ) {

    jPanel1 = new javax.swing.JPa nel();
    amountLbl = new javax.swing.JLa bel();
    jLabel2 = new javax.swing.JLa bel();
    jLabel3 = new javax.swing.JLa bel();
    paymentLbl = new javax.swing.JLa bel();
    display = new javax.swing.JLa bel();
    amount = new javax.swing.JTe xtField();
    termLbl = new javax.swing.JTe xtField();
    rateLbl = new javax.swing.JTe xtField();
    computePay = new javax.swing.JBu tton();
    clear = new javax.swing.JBu tton();
    exit = new javax.swing.JBu tton();
    jPanel2 = new javax.swing.JPa nel();
    jScrollPane1 = new javax.swing.JSc rollPane();
    displayField = new javax.swing.JTe xtArea();
    jPanel3 = new javax.swing.JPa nel();
    mnuBar = new javax.swing.JMe nuBar();
    jMenu1 = new javax.swing.JMe nu();
    mnu7rs = new javax.swing.JMe nuItem();
    mnu15yrs = new javax.swing.JMe nuItem();
    mnu30yrs = new javax.swing.JMe nuItem();

    setDefaultClose Operation(javax .swing.WindowCo nstants.EXIT_ON _CLOSE);
    setTitle("Mortg age Calculator");

    jPanel1.setBord er(javax.swing. BorderFactory.c reateTitledBord er("Loan Details"));

    amountLbl.setFo nt(new java.awt.Font(" Tahoma", 1, 12));
    amountLbl.setTe xt("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.setF ont(new java.awt.Font(" Tahoma", 1, 12));
    paymentLbl.setT ext("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.setT ext("Calculate" );
    computePay.addA ctionListener(n ew java.awt.event. ActionListener( ) {
    public void actionPerformed (java.awt.event .ActionEvent evt) {
    computePayActio nPerformed(evt) ;
    }
    });

    clear.setText(" Clear");
    clear.addAction Listener(new java.awt.event. ActionListener( ) {
    public void actionPerformed (java.awt.event .ActionEvent evt) {
    clearActionPerf ormed(evt);
    }
    });

    exit.setText("E xit");
    exit.addActionL istener(new java.awt.event. ActionListener( ) {
    public void actionPerformed (java.awt.event .ActionEvent evt) {
    exitActionPerfo rmed(evt);
    }
    });

    javax.swing.Gro upLayout jPanel1Layout = new javax.swing.Gro upLayout(jPanel 1);
    jPanel1.setLayo ut(jPanel1Layou t);
    jPanel1Layout.s etHorizontalGro up(
    jPanel1Layout.c reateParallelGr oup(javax.swing .GroupLayout.Al ignment.LEADING )
    .addGroup(jPane l1Layout.create SequentialGroup ()
    .addContainerGa p()
    .addGroup(jPane l1Layout.create ParallelGroup(j avax.swing.Grou pLayout.Alignme nt.LEADING)
    .addGroup(jPane l1Layout.create SequentialGroup ()
    .addGroup(jPane l1Layout.create ParallelGroup(j avax.swing.Grou pLayout.Alignme nt.LEADING)
    .addComponent(a mountLbl, javax.swing.Gro upLayout.Alignm ent.TRAILING)
    .addComponent(j Label2, javax.swing.Gro upLayout.Alignm ent.TRAILING)
    .addComponent(j Label3, javax.swing.Gro upLayout.Alignm ent.TRAILING)
    .addComponent(p aymentLbl, javax.swing.Gro upLayout.Alignm ent.TRAILING))
    .addPreferredGa p(javax.swing.L ayoutStyle.Comp onentPlacement. RELATED)
    .addGroup(jPane l1Layout.create ParallelGroup(j avax.swing.Grou pLayout.Alignme nt.LEADING)
    .addComponent(t ermLbl, javax.swing.Gro upLayout.DEFAUL T_SIZE, 112, Short.MAX_VALUE )
    .addComponent(r ateLbl, javax.swing.Gro upLayout.DEFAUL T_SIZE, 112, Short.MAX_VALUE )
    .addComponent(d isplay, javax.swing.Gro upLayout.DEFAUL T_SIZE, 112, Short.MAX_VALUE )
    .addComponent(a mount, javax.swing.Gro upLayout.DEFAUL T_SIZE, 112, Short.MAX_VALUE )))
    .addGroup(jPane l1Layout.create SequentialGroup ()
    .addComponent(c omputePay)
    .addPreferredGa p(javax.swing.L ayoutStyle.Comp onentPlacement. RELATED)
    .addComponent(c lear)
    .addPreferredGa p(javax.swing.L ayoutStyle.Comp onentPlacement. RELATED)
    .addComponent(e xit, javax.swing.Gro upLayout.PREFER RED_SIZE, 66, javax.swing.Gro upLayout.PREFER RED_SIZE)))
    .addContainerGa p())
    );

    jPanel1Layout.l inkSize(javax.s wing.SwingConst ants.HORIZONTAL , new java.awt.Compon ent[] {clear, computePay, exit});

    jPanel1Layout.s etVerticalGroup (
    jPanel1Layout.c reateParallelGr oup(javax.swing .GroupLayout.Al ignment.LEADING )
    .addGroup(jPane l1Layout.create SequentialGroup ()
    .addGroup(jPane l1Layout.create ParallelGroup(j avax.swing.Grou pLayout.Alignme nt.BASELINE)
    .addComponent(a mountLbl)
    .addComponent(a mount, javax.swing.Gro upLayout.PREFER RED_SIZE, javax.swing.Gro upLayout.DEFAUL T_SIZE, javax.swing.Gro upLayout.PREFER RED_SIZE))
    .addPreferredGa p(javax.swing.L ayoutStyle.Comp onentPlacement. RELATED)
    .addGroup(jPane l1Layout.create ParallelGroup(j avax.swing.Grou pLayout.Alignme nt.BASELINE)
    .addComponent(j Label2)
    .addComponent(t ermLbl, javax.swing.Gro upLayout.PREFER RED_SIZE, javax.swing.Gro upLayout.DEFAUL T_SIZE, javax.swing.Gro upLayout.PREFER RED_SIZE))
    .addPreferredGa p(javax.swing.L ayoutStyle.Comp onentPlacement. RELATED)
    .addGroup(jPane l1Layout.create ParallelGroup(j avax.swing.Grou pLayout.Alignme nt.TRAILING)
    .addComponent(j Label3)
    .addComponent(r ateLbl, javax.swing.Gro upLayout.PREFER RED_SIZE, javax.swing.Gro upLayout.DEFAUL T_SIZE, javax.swing.Gro upLayout.PREFER RED_SIZE))
    .addPreferredGa p(javax.swing.L ayoutStyle.Comp onentPlacement. RELATED)
    .addGroup(jPane l1Layout.create ParallelGroup(j avax.swing.Grou pLayout.Alignme nt.BASELINE)
    .addComponent(p aymentLbl)
    .addComponent(d isplay))
    .addGap(18, 18, 18)
    .addGroup(jPane l1Layout.create ParallelGroup(j avax.swing.Grou pLayout.Alignme nt.BASELINE)
    .addComponent(c omputePay)
    .addComponent(c lear)
    .addComponent(e xit))
    .addContainerGa p(100, Short.MAX_VALUE ))
    );

    jPanel2.setBord er(javax.swing. BorderFactory.c reateTitledBord er("Amortizatio n Table\n"));

    displayField.se tColumns(20);
    displayField.se tRows(5);
    jScrollPane1.se tViewportView(d isplayField);

    javax.swing.Gro upLayout jPanel2Layout = new javax.swing.Gro upLayout(jPanel 2);
    jPanel2.setLayo ut(jPanel2Layou t);
    jPanel2Layout.s etHorizontalGro up(
    jPanel2Layout.c reateParallelGr oup(javax.swing .GroupLayout.Al ignment.LEADING )
    .addGroup(jPane l2Layout.create SequentialGroup ()
    .addContainerGa p()
    .addComponent(j ScrollPane1, javax.swing.Gro upLayout.DEFAUL T_SIZE, 375, Short.MAX_VALUE )
    .addContainerGa p())
    );
    jPanel2Layout.s etVerticalGroup (
    jPanel2Layout.c reateParallelGr oup(javax.swing .GroupLayout.Al ignment.LEADING )
    .addGroup(jPane l2Layout.create SequentialGroup ()
    .addComponent(j ScrollPane1, javax.swing.Gro upLayout.DEFAUL T_SIZE, 226, Short.MAX_VALUE )
    .addContainerGa p())
    );

    jPanel3.setBord er(javax.swing. BorderFactory.c reateTitledBord er("Loan Chart\n"));

    javax.swing.Gro upLayout jPanel3Layout = new javax.swing.Gro upLayout(jPanel 3);
    jPanel3.setLayo ut(jPanel3Layou t);
    jPanel3Layout.s etHorizontalGro up(
    jPanel3Layout.c reateParallelGr oup(javax.swing .GroupLayout.Al ignment.LEADING )
    .addGap(0, 680, Short.MAX_VALUE )
    );
    jPanel3Layout.s etVerticalGroup (
    jPanel3Layout.c reateParallelGr oup(javax.swing .GroupLayout.Al ignment.LEADING )
    .addGap(0, 261, Short.MAX_VALUE )
    );

    jMenu1.setText( "Term");

    mnu7rs.setText( "7yrs @ 5.35%");
    mnu7rs.addActio nListener(new java.awt.event. ActionListener( ) {
    public void actionPerformed (java.awt.event .ActionEvent evt) {
    mnu7rsActionPer formed(evt);
    }
    });
    jMenu1.add(mnu7 rs);

    mnu15yrs.setTex t("15yrs @ 5.50%");
    mnu15yrs.addAct ionListener(new java.awt.event. ActionListener( ) {
    public void actionPerformed (java.awt.event .ActionEvent evt) {
    mnu15yrsActionP erformed(evt);
    }
    });
    jMenu1.add(mnu1 5yrs);

    mnu30yrs.setTex t("30yrs @ 5.75%");
    mnu30yrs.addAct ionListener(new java.awt.event. ActionListener( ) {
    public void actionPerformed (java.awt.event .ActionEvent evt) {
    mnu30yrsActionP erformed(evt);
    }
    });
    jMenu1.add(mnu3 0yrs);

    mnuBar.add(jMen u1);

    setJMenuBar(mnu Bar);

    javax.swing.Gro upLayout layout = new javax.swing.Gro upLayout(getCon tentPane());
    getContentPane( ).setLayout(lay out);
    layout.setHoriz ontalGroup(
    layout.createPa rallelGroup(jav ax.swing.GroupL ayout.Alignment .LEADING)
    .addGroup(layou t.createSequent ialGroup()
    .addContainerGa p()
    .addGroup(layou t.createParalle lGroup(javax.sw ing.GroupLayout .Alignment.TRAI LING)
    .addComponent(j Panel3, javax.swing.Gro upLayout.Alignm ent.LEADING, javax.swing.Gro upLayout.DEFAUL T_SIZE, javax.swing.Gro upLayout.DEFAUL T_SIZE, Short.MAX_VALUE )
    .addGroup(javax .swing.GroupLay out.Alignment.L EADING, layout.createSe quentialGroup()
    .addComponent(j Panel1, javax.swing.Gro upLayout.PREFER RED_SIZE, javax.swing.Gro upLayout.DEFAUL T_SIZE, javax.swing.Gro upLayout.PREFER RED_SIZE)
    .addPreferredGa p(javax.swing.L ayoutStyle.Comp onentPlacement. RELATED)
    .addComponent(j Panel2, javax.swing.Gro upLayout.DEFAUL T_SIZE, javax.swing.Gro upLayout.DEFAUL T_SIZE, Short.MAX_VALUE )))
    .addContainerGa p(javax.swing.G roupLayout.DEFA ULT_SIZE, Short.MAX_VALUE ))
    );
    layout.setVerti calGroup(
    layout.createPa rallelGroup(jav ax.swing.GroupL ayout.Alignment .LEADING)
    .addGroup(layou t.createSequent ialGroup()
    .addContainerGa p()
    .addGroup(layou t.createParalle lGroup(javax.sw ing.GroupLayout .Alignment.LEAD ING, false)
    .addComponent(j Panel2, javax.swing.Gro upLayout.DEFAUL T_SIZE, javax.swing.Gro upLayout.DEFAUL T_SIZE, Short.MAX_VALUE )
    .addComponent(j Panel1, javax.swing.Gro upLayout.DEFAUL T_SIZE, javax.swing.Gro upLayout.DEFAUL T_SIZE, Short.MAX_VALUE ))
    .addPreferredGa p(javax.swing.L ayoutStyle.Comp onentPlacement. RELATED)
    .addComponent(j Panel3, javax.swing.Gro upLayout.DEFAUL T_SIZE, javax.swing.Gro upLayout.DEFAUL T_SIZE, Short.MAX_VALUE )
    .addContainerGa p())
    );

    pack();
    }// </editor-fold>//GEN-END:initCompone nts

    private void clearActionPerf ormed(java.awt. event.ActionEve nt evt) {//GEN-FIRST:event_cle arActionPerform ed
    amount.setText( "");
    display.setText ("");
    displayField.se tText("");
    termLbl.setText ("");
    rateLbl.setText ("");
    }//GEN-LAST:event_clea rActionPerforme d

    private void exitActionPerfo rmed(java.awt.e vent.ActionEven t evt) {//GEN-FIRST:event_exi tActionPerforme d
    System.exit(1);
    }//GEN-LAST:event_exit ActionPerformed

    private void computePayActio nPerformed(java .awt.event.Acti onEvent evt) {//GEN-FIRST:event_com putePayActionPe rformed
    try {

    // calculate the monthly payment
    double p=Double.parseD ouble(amount.ge tText());
    double r=Double.parseD ouble(rateLbl.g etText())/1200;
    double n=Integer.parse Int(termLbl.get Text())*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(mont hlyPayment));
    // calculate the detailed loan
    double principal=p;
    int month;
    StringBuffer buffer=new StringBuffer();
    buffer.append(" Month\tAmount\t Interest\tBalan ce\n");
    for (int i=0; i<n; i++) {
    month=i+1;
    double interest=princi pal*r;
    double balance=princip al+interest-monthlyPayment;
    buffer.append(m onth+"\t");
    buffer.append(n ew String(df.forma t(principal))+" \t");
    buffer.append(n ew String(df.forma t(interest))+"\ t");
    buffer.append(n ew String(df.forma t(balance))+"\n ");
    principal=balan ce;
    }
    displayField.se tText(buffer.to String());
    } catch(Exception ex) {
    System.out.prin tln(ex);
    }

    }//GEN-LAST:event_comp utePayActionPer formed

    public void getRates(){ // START OF NEW CODE
    try{
    ArrayList<Strin g> InterestRates = new ArrayList<Strin g>();

    BufferedReader inputfile
    = new BufferedReader( new FileReader("rat es.txt"));
    String data;

    // debugged by hiwa 1 Jun. 2006
    while ((data = inputfile.readL ine()) != null){
    System.out.prin tln(data);
    InterestRates.a dd(data);
    }

    rates = new double[InterestRates.s ize()];
    for (int x = 0; x < rates.length; ++x){
    rates[x] = Double.parseDou ble(InterestRat es.get(x));
    }

    inputfile.close ();
    }
    catch(Exception ec)
    {
    }
    }



    private void mnu7rsActionPer formed(java.awt .event.ActionEv ent evt) {//GEN-FIRST:event_mnu 7rsActionPerfor med
    termLbl.setText ("" + iTerms[0]);
    rateLbl.setText ("" + rates[0]);
    }//GEN-LAST:event_mnu7 rsActionPerform ed

    private void mnu15yrsActionP erformed(java.a wt.event.Action Event evt) {//GEN-FIRST:event_mnu 15yrsActionPerf ormed
    termLbl.setText ("" + iTerms[1]);
    rateLbl.setText ("" + rates[x]);
    }//GEN-LAST:event_mnu1 5yrsActionPerfo rmed

    private void mnu30yrsActionP erformed(java.a wt.event.Action Event evt) {//GEN-FIRST:event_mnu 30yrsActionPerf ormed
    termLbl.setText ("" + iTerms[2]);
    rateLbl.setText ("" + rates[x]);
    }//GEN-LAST:event_mnu3 0yrsActionPerfo rmed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
    java.awt.EventQ ueue.invokeLate r(new Runnable() {
    public void run() {
    new MortFrame().set Visible(true);
    }
    });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    int iTerms[] = {7, 15, 30};
    double[] rates;
    int x;
    private javax.swing.JTe xtField amount;
    private javax.swing.JLa bel amountLbl;
    private javax.swing.JBu tton clear;
    private javax.swing.JBu tton computePay;
    private javax.swing.JLa bel display;
    private javax.swing.JTe xtArea displayField;
    private javax.swing.JBu tton exit;
    private javax.swing.JLa bel jLabel2;
    private javax.swing.JLa bel jLabel3;
    private javax.swing.JMe nu jMenu1;
    private javax.swing.JPa nel jPanel1;
    private javax.swing.JPa nel jPanel2;
    private javax.swing.JPa nel jPanel3;
    private javax.swing.JSc rollPane jScrollPane1;
    private javax.swing.JMe nuItem mnu15yrs;
    private javax.swing.JMe nuItem mnu30yrs;
    private javax.swing.JMe nuItem mnu7rs;
    private javax.swing.JMe nuBar mnuBar;
    private javax.swing.JLa bel paymentLbl;
    private javax.swing.JTe xtField rateLbl;
    private javax.swing.JTe xtField termLbl;
    // End of variables declaration//GEN-END:variables

    }
    Last edited by r035198x; Feb 26 '08, 06:11 AM. Reason: Removed code tags!
  • stevedub
    New Member
    • Jan 2007
    • 40

    #2
    Hmm, for some reason my previous post is not showing up right for me. I'm just reposting just in case something is wrong.

    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:
    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.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Mortgage Calculator");
    
            jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("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.GroupLayout.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.ComponentPlacement.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.ComponentPlacement.RELATED)
                            .addComponent(clear)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.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.GroupLayout.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.ComponentPlacement.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.ComponentPlacement.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.ComponentPlacement.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.createTitledBorder("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.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
                    .addContainerGap())
            );
            jPanel2Layout.setVerticalGroup(
                jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
                    .addContainerGap())
            );
    
            jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Loan Chart\n"));
    
            javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
            jPanel3.setLayout(jPanel3Layout);
            jPanel3Layout.setHorizontalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGap(0, 680, Short.MAX_VALUE)
            );
            jPanel3Layout.setVerticalGroup(
                jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.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.GroupLayout.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.LEADING, layout.createSequentialGroup()
                            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.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.ComponentPlacement.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.ActionEvent 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
    
    }

    Comment

    Working...