Help with GUI, Array, and Reading File
Hi, I am back again. I need help with a GUI and how to implement an array from a sequential file. I wrote the sequential file in a notepad text editor, intRates.txt. I want the values in the file to be read into the array. I want to use the values of the array to be inserted into places where I use intRate as a variable. I will post my code. Any suggestions are excellent. I appreciate it.
Code:
package mortgage;
/**
*
* @author Lekeisha
*/
import java.util.Scanner;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MortgageGUI extends javax.swing.JFrame {
/** Creates new form MortgageGUI */
public MortgageGUI() {
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.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jPanel1 = new javax.swing.JPanel();
mortgageTextField1 = new javax.swing.JTextField();
loantermTextField2 = new javax.swing.JTextField();
interestrateTextField3 = new javax.swing.JTextField();
mortgagecalculatedTextField4 = new javax.swing.JTextField();
loancalculatedTextField5 = new javax.swing.JTextField();
interestcalculatedTextField6 = new javax.swing.JTextField();
mortgageLabel1 = new javax.swing.JLabel();
loantermLabel3 = new javax.swing.JLabel();
interestrateLabel5 = new javax.swing.JLabel();
jLabel7 = new javax.swing.JLabel();
calculateButton7 = new javax.swing.JButton();
clearjButton8 = new javax.swing.JButton();
jLabel9 = new javax.swing.JLabel();
jLabel11 = new javax.swing.JLabel();
jMenuBar1 = new javax.swing.JMenuBar();
jMenu1 = new javax.swing.JMenu();
jMenuItem1 = new javax.swing.JMenuItem();
jMenuItem2 = new javax.swing.JMenuItem();
jMenuItem3 = new javax.swing.JMenuItem();
jMenu2 = new javax.swing.JMenu();
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 100, Short.MAX_VALUE)
);
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Mortgage Calculator");
mortgageTextField1.setText("200000");
mortgageTextField1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mortgageTextField1ActionPerformed(evt);
}
});
loantermTextField2.setText("30");
loantermTextField2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loantermTextField2ActionPerformed(evt);
}
});
interestrateTextField3.setText(".0575");
interestrateTextField3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
interestrateTextField3ActionPerformed(evt);
}
});
mortgagecalculatedTextField4.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
mortgagecalculatedTextField4ActionPerformed(evt);
}
});
loancalculatedTextField5.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
loancalculatedTextField5ActionPerformed(evt);
}
});
interestcalculatedTextField6.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
interestcalculatedTextField6ActionPerformed(evt);
}
});
mortgageLabel1.setText("Mortgage Amount");
loantermLabel3.setText("Loan Term");
interestrateLabel5.setText("Interest Rate");
jLabel7.setText("Mortgage Amount");
calculateButton7.setText("Calculate");
calculateButton7.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
calculateButton7ActionPerformed(evt);
}
});
clearjButton8.setText("Clear");
clearjButton8.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
clearjButton8ActionPerformed(evt);
}
});
jLabel9.setText("Loan Balance");
jLabel11.setText("Interest Amount");
jMenu1.setText("File");
jMenuItem1.setText("7yrs at 5.35 %");
jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuItem2.setText("15yrs at 5.5%");
jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem2ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem2);
jMenuItem3.setText("30yrs at 5.75%");
jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jMenuItem3ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem3);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
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(77, Short.MAX_VALUE)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(mortgageLabel1)
.addComponent(jLabel7)
.addComponent(mortgageTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(mortgagecalculatedTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel9, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(loantermLabel3)
.addComponent(loantermTextField2)
.addComponent(loancalculatedTextField5))
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel11)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(interestrateTextField3, javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(interestrateLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(interestcalculatedTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(239, 239, 239)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
.addComponent(calculateButton7, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(clearjButton8, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGap(108, 108, 108))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(loantermLabel3)
.addComponent(interestrateLabel5)
.addComponent(mortgageLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(loantermTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(interestrateTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(mortgageTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(20, 20, 20))
.addComponent(calculateButton7, javax.swing.GroupLayout.Alignment.TRAILING))
.addGap(40, 40, 40)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel9)
.addComponent(jLabel11)
.addComponent(jLabel7))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(loancalculatedTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(interestcalculatedTextField6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(mortgagecalculatedTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(clearjButton8)
.addContainerGap(80, Short.MAX_VALUE))
);
pack();
}// </editor-fold>
public static void main (String[]args ) throw IOException
{
final int ROWS = 2;
int Counter = 0;
File f = new File("intRates.txt");
if (!(f.exists()))
{
System.out.println("Program is closing");
System.exit(0);
}
try {
Scanner InputFile = new Scanner (f);
} catch (FileNotFoundException ex) {
Logger.getLogger(MortgageGUI.class.getName()).log(Level.SEVERE, null, ex);
}
int [] intRates = new int [ROWS];
}
private void mortgagecalculatedTextField4ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println();
//calculates the mortgage payment amount based on amount entered by user // TODO add your handling code here:
}
private void calculateButton7ActionPerformed(java.awt.event.ActionEvent evt) {
//initiates the monthly interest rate
//initiates the mortgage payment
//initiates interest paid
//initiates principal paid
double intRates = Double.parseDouble(interestrateTextField3.getText());
double monIntRate = intRates/12; //interest rate divided by 12 months equals the monthly interest rate
double loanTerm = Double.parseDouble(loantermTextField2.getText());
double totMons = loanTerm*12; //term of loan multiplied by 12 months equals total months of term
double amLoanOne = Double.parseDouble(mortgageTextField1.getText());
//initiates the amount of loan balance after initial amount
double monPay = amLoanOne*monIntRate/(1-(Math.pow((1+monIntRate), (-totMons))));
mortgagecalculatedTextField4.setText(Math.round(monPay) + "");
//calculates the mortgage payment
double intsPaid = amLoanOne*monIntRate;
interestcalculatedTextField6.setText(Math.round(intsPaid) + "");
//calculates the interest paid
double princPaid = monPay - intsPaid;
//calculates the principal paid
double amLoanNxt = amLoanOne - princPaid;
loancalculatedTextField5.setText(Math.round(amLoanNxt) + "");
//calculates the amount of loan balance after initial amount
}
private void clearjButton8ActionPerformed(java.awt.event.ActionEvent evt) {
mortgageTextField1.setText("");
loantermTextField2.setText("");
interestrateTextField3.setText("");
mortgagecalculatedTextField4.setText("");
loancalculatedTextField5.setText("");
interestcalculatedTextField6.setText("");
// TODO add your handling code here:
}
private void mortgageTextField1ActionPerformed(java.awt.event.ActionEvent evt) {
String amLoanOne = mortgageTextField1.getText(); // TODO add your handling code here:
}
private void loantermTextField2ActionPerformed(java.awt.event.ActionEvent evt) {
String loanTerm = loantermTextField2.getText(); // TODO add your handling code here:
}
private void interestrateTextField3ActionPerformed(java.awt.event.ActionEvent evt) {
String intRates = interestrateTextField3.getText(); // TODO add your handling code here:
}
private void loancalculatedTextField5ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println();
//calculates the loan balance based on the amount entered by user // TODO add your handling code here:
}
private void interestcalculatedTextField6ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println();
//calculates the interest paid with interest rate entered by user // TODO add your handling code here:
}
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
double [] intRates = new double[2];
for (int i =0; i < 2; i++)
{
double monIntRate = .0535/12; //interest rate divided by 12 months equals the monthly interest rate
double loanTerm = 7.;
double totMons = loanTerm*12; //term of loan multiplied by 12 months equals total months of term
double amLoanOne = Double.parseDouble(mortgageTextField1.getText());
//initiates the amount of loan balance after initial amount
double monPay = amLoanOne*monIntRate/(1-(Math.pow((1+monIntRate), (-totMons))));
mortgagecalculatedTextField4.setText(Math.round(monPay) + "");
//calculates the mortgage payment
double intsPaid = amLoanOne*monIntRate;
interestcalculatedTextField6.setText(Math.round(intsPaid) + "");
//calculates the interest paid
double princPaid = monPay - intsPaid;
//calculates the principal paid
double amLoanNxt = amLoanOne - princPaid;
loancalculatedTextField5.setText(Math.round(amLoanNxt) + "");
}
//calculates the amount of loan balance after initial amount
// TODO add your handling code here:
}
private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {
double monIntRate = .0550/12; //interest rate divided by 12 months equals the monthly interest rate
double totMons = 15.*12; //term of loan multiplied by 12 months equals total months of term
double amLoanOne = Double.parseDouble(mortgageTextField1.getText());
//initiates the amount of loan balance after initial amount
double monPay = amLoanOne*monIntRate/(1-(Math.pow((1+monIntRate), (-totMons))));
mortgagecalculatedTextField4.setText(Math.round(monPay) + "");
//calculates the mortgage payment
double intsPaid = amLoanOne*monIntRate;
interestcalculatedTextField6.setText(Math.round(intsPaid) + "");
//calculates the interest paid
double princPaid = monPay - intsPaid;
//calculates the principal paid
double amLoanNxt = amLoanOne - princPaid;
loancalculatedTextField5.setText(Math.round(amLoanNxt) + "");
//calculates the amount of loan balance after initial amount
// TODO add your handling code here: // TODO add your handling code here:
}
private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {
double monIntRate = .0575/12; //interest rate divided by 12 months equals the monthly interest rate
double totMons = 30.*12; //term of loan multiplied by 12 months equals total months of term
double amLoanOne = Double.parseDouble(mortgageTextField1.getText());
//initiates the amount of loan balance after initial amount
double monPay = amLoanOne*monIntRate/(1-(Math.pow((1+monIntRate), (-totMons))));
mortgagecalculatedTextField4.setText(Math.round(monPay) + "");
//calculates the mortgage payment
double intsPaid = amLoanOne*monIntRate;
interestcalculatedTextField6.setText(Math.round(intsPaid) + "");
//calculates the interest paid
double princPaid = monPay - intsPaid;
//calculates the principal paid
double amLoanNxt = amLoanOne - princPaid;
loancalculatedTextField5.setText(Math.round(amLoanNxt) + "");
//calculates the amount of loan balance after initial amount
// TODO add your handling code here: // TODO add your handling code here: // TODO add your handling code here:
}
/**
* @param args the command line arguments
*/
{
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MortgageGUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton calculateButton7;
private javax.swing.JButton clearjButton8;
private javax.swing.JTextField interestcalculatedTextField6;
private javax.swing.JLabel interestrateLabel5;
private javax.swing.JTextField interestrateTextField3;
private javax.swing.JLabel jLabel11;
private javax.swing.JLabel jLabel7;
private javax.swing.JLabel jLabel9;
private javax.swing.JMenu jMenu1;
private javax.swing.JMenu jMenu2;
private javax.swing.JMenuBar jMenuBar1;
private javax.swing.JMenuItem jMenuItem1;
private javax.swing.JMenuItem jMenuItem2;
private javax.swing.JMenuItem jMenuItem3;
private javax.swing.JPanel jPanel1;
private javax.swing.JTextField loancalculatedTextField5;
private javax.swing.JLabel loantermLabel3;
private javax.swing.JTextField loantermTextField2;
private javax.swing.JLabel mortgageLabel1;
private javax.swing.JTextField mortgageTextField1;
private javax.swing.JTextField mortgagecalculatedTextField4;
// End of variables declaration
}
Help with GUI, Array, and Reading File
Here is what my application is not doing. It builds, but it does not show the GUI. It just shows "Program is closing". I think if I can figure where I am going wrong with that, the rest will come along. I fixed where my array accesses the elements. I need help with that part. Thanks.