473,503 Members | 2,165 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem reading a sequential file and creating an array

40 New Member
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

}
Feb 26 '08 #1
1 2901
stevedub
40 New Member
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:
Expand|Select|Wrap|Line Numbers
  1. /*
  2.  * MortFrame.java
  3.  *
  4.  * Created on February 24, 2008, 7:28 PM
  5.  */
  6.  
  7.  
  8. import java.io.File;
  9. import java.io.InputStream;
  10. import java.io.FileInputStream;
  11. import java.io.IOException;
  12.  
  13. import java.io.*;
  14. import javax.swing.*;
  15. import javax.swing.event.*;
  16. import java.awt.*;
  17. import java.awt.event.*;
  18. import java.text.*;
  19. import java.util.*;
  20.  
  21. public class MortFrame extends javax.swing.JFrame {
  22.  
  23.     /** Creates new form MortFrame */
  24.     public MortFrame() {
  25.         initComponents();
  26.     }
  27.  
  28.     /** This method is called from within the constructor to
  29.      * initialize the form.
  30.      * WARNING: Do NOT modify this code. The content of this method is
  31.      * always regenerated by the Form Editor.
  32.      */
  33.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  34.     private void initComponents() {
  35.  
  36.         jPanel1 = new javax.swing.JPanel();
  37.         amountLbl = new javax.swing.JLabel();
  38.         jLabel2 = new javax.swing.JLabel();
  39.         jLabel3 = new javax.swing.JLabel();
  40.         paymentLbl = new javax.swing.JLabel();
  41.         display = new javax.swing.JLabel();
  42.         amount = new javax.swing.JTextField();
  43.         termLbl = new javax.swing.JTextField();
  44.         rateLbl = new javax.swing.JTextField();
  45.         computePay = new javax.swing.JButton();
  46.         clear = new javax.swing.JButton();
  47.         exit = new javax.swing.JButton();
  48.         jPanel2 = new javax.swing.JPanel();
  49.         jScrollPane1 = new javax.swing.JScrollPane();
  50.         displayField = new javax.swing.JTextArea();
  51.         jPanel3 = new javax.swing.JPanel();
  52.         mnuBar = new javax.swing.JMenuBar();
  53.         jMenu1 = new javax.swing.JMenu();
  54.         mnu7rs = new javax.swing.JMenuItem();
  55.         mnu15yrs = new javax.swing.JMenuItem();
  56.         mnu30yrs = new javax.swing.JMenuItem();
  57.  
  58.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  59.         setTitle("Mortgage Calculator");
  60.  
  61.         jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Loan Details"));
  62.  
  63.         amountLbl.setFont(new java.awt.Font("Tahoma", 1, 12));
  64.         amountLbl.setText("Loan Amount: $");
  65.  
  66.         jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
  67.         jLabel2.setText("Term:");
  68.  
  69.         jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12));
  70.         jLabel3.setText("Rate");
  71.  
  72.         paymentLbl.setFont(new java.awt.Font("Tahoma", 1, 12));
  73.         paymentLbl.setText("Monthly Payment: $");
  74.  
  75.         display.setFont(new java.awt.Font("Tahoma", 0, 12));
  76.  
  77.         amount.setFont(new java.awt.Font("Tahoma", 0, 12));
  78.  
  79.         termLbl.setFont(new java.awt.Font("Tahoma", 0, 12));
  80.  
  81.         rateLbl.setFont(new java.awt.Font("Tahoma", 0, 12));
  82.  
  83.         computePay.setText("Calculate");
  84.         computePay.addActionListener(new java.awt.event.ActionListener() {
  85.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  86.                 computePayActionPerformed(evt);
  87.             }
  88.         });
  89.  
  90.         clear.setText("Clear");
  91.         clear.addActionListener(new java.awt.event.ActionListener() {
  92.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  93.                 clearActionPerformed(evt);
  94.             }
  95.         });
  96.  
  97.         exit.setText("Exit");
  98.         exit.addActionListener(new java.awt.event.ActionListener() {
  99.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  100.                 exitActionPerformed(evt);
  101.             }
  102.         });
  103.  
  104.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  105.         jPanel1.setLayout(jPanel1Layout);
  106.         jPanel1Layout.setHorizontalGroup(
  107.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  108.             .addGroup(jPanel1Layout.createSequentialGroup()
  109.                 .addContainerGap()
  110.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  111.                     .addGroup(jPanel1Layout.createSequentialGroup()
  112.                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  113.                             .addComponent(amountLbl, javax.swing.GroupLayout.Alignment.TRAILING)
  114.                             .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
  115.                             .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
  116.                             .addComponent(paymentLbl, javax.swing.GroupLayout.Alignment.TRAILING))
  117.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  118.                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  119.                             .addComponent(termLbl, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
  120.                             .addComponent(rateLbl, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
  121.                             .addComponent(display, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)
  122.                             .addComponent(amount, javax.swing.GroupLayout.DEFAULT_SIZE, 112, Short.MAX_VALUE)))
  123.                     .addGroup(jPanel1Layout.createSequentialGroup()
  124.                         .addComponent(computePay)
  125.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  126.                         .addComponent(clear)
  127.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  128.                         .addComponent(exit, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)))
  129.                 .addContainerGap())
  130.         );
  131.  
  132.         jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {clear, computePay, exit});
  133.  
  134.         jPanel1Layout.setVerticalGroup(
  135.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  136.             .addGroup(jPanel1Layout.createSequentialGroup()
  137.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  138.                     .addComponent(amountLbl)
  139.                     .addComponent(amount, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  140.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  141.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  142.                     .addComponent(jLabel2)
  143.                     .addComponent(termLbl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  144.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  145.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  146.                     .addComponent(jLabel3)
  147.                     .addComponent(rateLbl, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  148.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  149.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  150.                     .addComponent(paymentLbl)
  151.                     .addComponent(display))
  152.                 .addGap(18, 18, 18)
  153.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  154.                     .addComponent(computePay)
  155.                     .addComponent(clear)
  156.                     .addComponent(exit))
  157.                 .addContainerGap(100, Short.MAX_VALUE))
  158.         );
  159.  
  160.         jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Amortization Table\n"));
  161.  
  162.         displayField.setColumns(20);
  163.         displayField.setRows(5);
  164.         jScrollPane1.setViewportView(displayField);
  165.  
  166.         javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
  167.         jPanel2.setLayout(jPanel2Layout);
  168.         jPanel2Layout.setHorizontalGroup(
  169.             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  170.             .addGroup(jPanel2Layout.createSequentialGroup()
  171.                 .addContainerGap()
  172.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 375, Short.MAX_VALUE)
  173.                 .addContainerGap())
  174.         );
  175.         jPanel2Layout.setVerticalGroup(
  176.             jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  177.             .addGroup(jPanel2Layout.createSequentialGroup()
  178.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
  179.                 .addContainerGap())
  180.         );
  181.  
  182.         jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Loan Chart\n"));
  183.  
  184.         javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
  185.         jPanel3.setLayout(jPanel3Layout);
  186.         jPanel3Layout.setHorizontalGroup(
  187.             jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  188.             .addGap(0, 680, Short.MAX_VALUE)
  189.         );
  190.         jPanel3Layout.setVerticalGroup(
  191.             jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  192.             .addGap(0, 261, Short.MAX_VALUE)
  193.         );
  194.  
  195.         jMenu1.setText("Term");
  196.  
  197.         mnu7rs.setText("7yrs @ 5.35%");
  198.         mnu7rs.addActionListener(new java.awt.event.ActionListener() {
  199.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  200.                 mnu7rsActionPerformed(evt);
  201.             }
  202.         });
  203.         jMenu1.add(mnu7rs);
  204.  
  205.         mnu15yrs.setText("15yrs @ 5.50%");
  206.         mnu15yrs.addActionListener(new java.awt.event.ActionListener() {
  207.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  208.                 mnu15yrsActionPerformed(evt);
  209.             }
  210.         });
  211.         jMenu1.add(mnu15yrs);
  212.  
  213.         mnu30yrs.setText("30yrs @ 5.75%");
  214.         mnu30yrs.addActionListener(new java.awt.event.ActionListener() {
  215.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  216.                 mnu30yrsActionPerformed(evt);
  217.             }
  218.         });
  219.         jMenu1.add(mnu30yrs);
  220.  
  221.         mnuBar.add(jMenu1);
  222.  
  223.         setJMenuBar(mnuBar);
  224.  
  225.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  226.         getContentPane().setLayout(layout);
  227.         layout.setHorizontalGroup(
  228.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  229.             .addGroup(layout.createSequentialGroup()
  230.                 .addContainerGap()
  231.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  232.                     .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  233.                     .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
  234.                         .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  235.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  236.                         .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
  237.                 .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  238.         );
  239.         layout.setVerticalGroup(
  240.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  241.             .addGroup(layout.createSequentialGroup()
  242.                 .addContainerGap()
  243.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  244.                     .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  245.                     .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
  246.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  247.                 .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  248.                 .addContainerGap())
  249.         );
  250.  
  251.         pack();
  252.     }// </editor-fold>//GEN-END:initComponents
  253.  
  254.     private void clearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearActionPerformed
  255.         amount.setText("");
  256.         display.setText("");
  257.         displayField.setText("");
  258.         termLbl.setText("");
  259.         rateLbl.setText("");
  260. }//GEN-LAST:event_clearActionPerformed
  261.  
  262.     private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
  263.         System.exit(1);
  264. }//GEN-LAST:event_exitActionPerformed
  265.  
  266.     private void computePayActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_computePayActionPerformed
  267.         try {
  268.  
  269.             // calculate the monthly payment
  270.             double p=Double.parseDouble(amount.getText());
  271.             double r=Double.parseDouble(rateLbl.getText())/1200;
  272.             double n=Integer.parseInt(termLbl.getText())*12;
  273.             double monthlyPayment=p*Math.pow(1+r,n)*r/(Math.pow(1+r,n)-1);
  274.             DecimalFormat df = new DecimalFormat("$###,###.00");
  275.             display.setText(df.format(monthlyPayment));
  276.             // calculate the detailed loan
  277.             double principal=p;
  278.             int month;
  279.             StringBuffer buffer=new StringBuffer();
  280.             buffer.append("Month\tAmount\tInterest\tBalance\n");
  281.             for (int i=0; i<n; i++) {
  282.                 month=i+1;
  283.                 double interest=principal*r;
  284.                 double balance=principal+interest-monthlyPayment;
  285.                     buffer.append(month+"\t");
  286.                     buffer.append(new String(df.format(principal))+"\t");
  287.                     buffer.append(new String(df.format(interest))+"\t");
  288.                     buffer.append(new String(df.format(balance))+"\n");
  289.                     principal=balance;
  290.                     }
  291.                     displayField.setText(buffer.toString());
  292.                     } catch(Exception ex) {
  293.                             System.out.println(ex);
  294.                 }
  295.  
  296. }//GEN-LAST:event_computePayActionPerformed
  297.  
  298.     public void getRates(){ // START OF NEW CODE
  299.                     try{
  300.                       ArrayList<String> InterestRates = new ArrayList<String>();
  301.  
  302.                       BufferedReader inputfile
  303.                        = new BufferedReader(new FileReader("rates.txt"));
  304.                       String data;
  305.  
  306.                       // debugged by hiwa 1 Jun. 2006
  307.                       while ((data = inputfile.readLine()) != null){
  308.                         System.out.println(data);
  309.                         InterestRates.add(data);
  310.                       }
  311.  
  312.                       rates = new double[InterestRates.size()];
  313.                       for (int x = 0; x < rates.length; ++x){
  314.                      rates[x] = Double.parseDouble(InterestRates.get(x));
  315.     }
  316.  
  317.         inputfile.close();
  318.     }
  319.         catch(Exception ec)
  320.     {
  321.     }
  322. }
  323.  
  324.  
  325.  
  326.     private void mnu7rsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnu7rsActionPerformed
  327.         termLbl.setText("" + iTerms[0]);
  328.         rateLbl.setText("" + rates[0]);
  329. }//GEN-LAST:event_mnu7rsActionPerformed
  330.  
  331.     private void mnu15yrsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnu15yrsActionPerformed
  332.         termLbl.setText("" + iTerms[1]);
  333.         rateLbl.setText("" + rates[x]);
  334. }//GEN-LAST:event_mnu15yrsActionPerformed
  335.  
  336.     private void mnu30yrsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnu30yrsActionPerformed
  337.         termLbl.setText("" + iTerms[2]);
  338.         rateLbl.setText("" + rates[x]);
  339. }//GEN-LAST:event_mnu30yrsActionPerformed
  340.  
  341.     /**
  342.      * @param args the command line arguments
  343.      */
  344.     public static void main(String args[]) {
  345.         java.awt.EventQueue.invokeLater(new Runnable() {
  346.             public void run() {
  347.                 new MortFrame().setVisible(true);
  348.             }
  349.         });
  350.     }
  351.  
  352.     // Variables declaration - do not modify//GEN-BEGIN:variables
  353.     int iTerms[] = {7, 15, 30};
  354.     double[] rates;
  355.     int x;
  356.     private javax.swing.JTextField amount;
  357.     private javax.swing.JLabel amountLbl;
  358.     private javax.swing.JButton clear;
  359.     private javax.swing.JButton computePay;
  360.     private javax.swing.JLabel display;
  361.     private javax.swing.JTextArea displayField;
  362.     private javax.swing.JButton exit;
  363.     private javax.swing.JLabel jLabel2;
  364.     private javax.swing.JLabel jLabel3;
  365.     private javax.swing.JMenu jMenu1;
  366.     private javax.swing.JPanel jPanel1;
  367.     private javax.swing.JPanel jPanel2;
  368.     private javax.swing.JPanel jPanel3;
  369.     private javax.swing.JScrollPane jScrollPane1;
  370.     private javax.swing.JMenuItem mnu15yrs;
  371.     private javax.swing.JMenuItem mnu30yrs;
  372.     private javax.swing.JMenuItem mnu7rs;
  373.     private javax.swing.JMenuBar mnuBar;
  374.     private javax.swing.JLabel paymentLbl;
  375.     private javax.swing.JTextField rateLbl;
  376.     private javax.swing.JTextField termLbl;
  377.     // End of variables declaration//GEN-END:variables
  378.  
  379. }
  380.  
Feb 26 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
15054
by: Tommie Nygren | last post by:
For different reasons I am reading an array of bytes from a file in my ..jar-file and from this I want to create a PNG image using Image.createImage(myArray, 0 , myArray.length). I had a...
3
2450
by: Usenet User | last post by:
I am trying to read (and then save) a binary file which has certain data structures in it. (The file is in propritetary format produced by a 3rd party MFC application.) I know that those data...
0
2189
by: Slawomir Nasiadka | last post by:
Hi, I'am new to this group so I would like to say "Hello" everyone and here is my problem: I'm writing a simple application (code is at the end of this message) witch would list all mails...
3
3402
by: Zeke Zinzul | last post by:
Hi Guys & Geeks, What's the most elegant way of dealing with binary data and structures? Say I have this (which I actually do, a woo-hoo): struct Struct_IconHeader { byte width; byte...
1
4542
by: Don.Leri | last post by:
Hi, I have a logger.dll (unmanaged c++ dll compiled in vs2005). I have a C# interop to use that dll in managed code implemented in Interfaces.dll (used by other C# dlls). I also have a...
4
3224
by: David | last post by:
Hi, (Sorry for duplicate post, finger trouble before I finished...) using C# 1.1 I am writing a winform app that collects a dataset from a webservice. At the same time I collect the data,...
12
3381
by: Julian | last post by:
Hi, I am having problems with a function that I have been using in my program to read sentences from a 'command file' and parse them into commands. the surprising thing is that the program works...
5
1955
by: Will Honea | last post by:
I've hit an interesting trap trying to migrate data off an OS/2 server running version 7.2 (fp14) over to 8.2 on Linux. Seems that one table has a column defined in the DDL as "BIGINT NOT NULL...
3
2153
by: Ahmad Jalil Qarshi | last post by:
Hi, I have a text file having size about 2 GB. The text file format is like: Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values For example...
0
7093
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7287
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7353
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5596
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4689
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3170
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1521
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
401
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.