Good morning all. I'm having some difficulty in reading from a sequential file. I think I have my code set up properly, but when ran, it throws an exception which show me the error statement I specified. When I ran a debug, it does seem to be pulling the information into the specified arrays. I'm not sure if I have something coded wrong or if I just don't have the sequential files stored in the right place. Thanks, as always, for your help.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.text.*;
import java.util.Scanner;
public class MortgageCalculatorWk4 extends JFrame implements ActionListener
{
JTextField tMortgageAmount; //the field used for input of the principle
JTextField tPayment; //the field used to output monthly payment
JTextField tTerm; //the field used for input of the user's term
JTextField tRate; //the field used for input of the user's interest
JComboBox cbMortgageLoanOptions; //drop down menu for loan options
JTextArea taAmmortization; //text area for ammortization
JScrollPane spAmmortization; //scroll bar for ammortization
protected JButton bCalculate; //calculate button
protected JButton bQuit; //quit button
JLabel lMessages; //label for messages
DecimalFormat df = new DecimalFormat("$###.00"); //format currency
private int mortgage; //principal
private int years; //term
private double rate; //interest rate
private double monthlyPaymentAmt; //mortgage payment amount
private double remainingBalance; //remaining balance
int [] arrLoanTerm; //term array
double[] arrLoanRate; //interest array
private double userRate; //user declared interest rate
private double userRate2; //user declared interest rate
private int userTerm; //user declared term
private int userTerm2; //user declared term
public MortgageCalculatorWk4() {
try {
BufferedReader scanFile = new BufferedReader(new FileReader("C:/array.txt"));
for(int i=0; i< arrLoanTerm.length; i++){
arrLoanTerm[i] = scanFile.read();
}
}
catch (Exception ex){
}
try {
Scanner scanFile2 = new Scanner(new File("C:/array2.txt"));
for(int i=0; i<arrLoanRate.length; i++){
arrLoanRate[i] = scanFile2.nextDouble();
}
}
catch (Exception ex){
}
//Create Messages
JPanel pMessages = new JPanel();
pMessages.setLayout(new FlowLayout());
pMessages.add(lMessages = new JLabel("If You Would Like to Calculate a Mortgage Using Unspecified Interest/Term, Please Select 'Own Terms' From the Drop-Down Menu"));
String[] loanOptions = {"7 years at 5.35%","15 years at 5.5%","30 years at 5.75%"};
//Create Labels
JPanel pLabels = new JPanel();
pLabels.setLayout(new GridLayout(6, 1));
pLabels.add(new JLabel("Enter Loan Amount"));
pLabels.add(new JLabel("Choose your mortgage terms"));
pLabels.add(new JLabel("Term"));
pLabels.add(new JLabel("Interest Rate"));
pLabels.add(new JLabel("Your Monthly Payment is:"));
taAmmortization = new JTextArea(30,20);
//Create Text Fields
JPanel pFields = new JPanel();
pFields.setLayout(new GridLayout(6, 1));
pFields.add(tMortgageAmount = new JTextField(12));
pFields.add(cbMortgageLoanOptions = new JComboBox(loanOptions));
pFields.add(tTerm = new JTextField(12));
pFields.add(tRate = new JTextField(12));
pFields.add(tPayment = new JTextField(12));
//Combine Labels & Text
JPanel pData = new JPanel();
pData.setLayout(new BorderLayout());
pData.add(pLabels, BorderLayout.WEST);
pData.add(pFields, BorderLayout.CENTER);
//Create Buttons
JPanel pButtons = new JPanel();
pButtons.setLayout(new FlowLayout());
pButtons.add(bCalculate = new JButton("Calculate"));
pButtons.add(bQuit = new JButton("Quit"));
//Create Scroll Bar
spAmmortization = new JScrollPane(taAmmortization);
spAmmortization.setPreferredSize(new Dimension(250,375));
spAmmortization.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
//Organize GUI
getContentPane().setLayout(new BorderLayout());
getContentPane().add(pMessages, BorderLayout.NORTH);
getContentPane().add(pData, BorderLayout.CENTER);
getContentPane().add(pButtons, BorderLayout.EAST);
getContentPane().add(spAmmortization,BorderLayout.SOUTH);
//Add Listeners
bCalculate.addActionListener(this);
bQuit.addActionListener(this);
tPayment.setEnabled(false);
taAmmortization.setEnabled(false);
}
//Event Handling
public void actionPerformed(ActionEvent e){
if (e.getSource() == bCalculate){
try{
String userInput;
userInput = tMortgageAmount.getText();
mortgage = Integer.parseInt(userInput);
rate = arrLoanRate[cbMortgageLoanOptions.getSelectedIndex()]/100 ;
years = arrLoanTerm[cbMortgageLoanOptions.getSelectedIndex()];
//if (years == 0){
//String userRate;
//userRate = tRate.getText();
//userRate2 = Double.parseDouble(userRate);
//String userTerm;
//userTerm = tTerm.getText();
//userTerm2 = Integer.parseInt(userTerm);
//rate = userRate2 / 100;
//years = userTerm2;
//MonthlyPaymentCalculation();
//tPayment.setText(String.valueOf(df.format(monthlyPaymentAmt)));
//lMessages.setText("If You Would Like to Calculate a Mortgage Using Unspecified Interest/Term, Please Select 'Own Terms' From the Drop-Down Menu");
//remainingBalance = mortgage;
//taAmmortization.setText("");
//LoanDetailsCalculation();
///repaint();
//else{
MonthlyPaymentCalculation();
tPayment.setText(String.valueOf(df.format(monthlyPaymentAmt)));
lMessages.setText("If You Would Like to Calculate a Mortgage Using Unspecified Interest/Term, Please Select 'Own Terms' From the Drop-Down Menu");
remainingBalance = mortgage;
taAmmortization.setText("");
LoanDetailsCalculation();
repaint();
}
catch (Exception ex) {
lMessages.setText("Invalid Entry. Please try again.");
}
} else {
System.exit(0);
}
}
public void LoanDetailsCalculation(){
taAmmortization.append("Month No.\tMonthly Payment\tRemaining Balance\tInterest Paid\n");
for (int i = 1; i <= years*12; i++){
double thisPaymentInterest = (rate / 12) * remainingBalance; // calculate the interest for the current payment
double thisPaymentPrinciple = monthlyPaymentAmt - thisPaymentInterest; // calculate the principle amount for the current payment
remainingBalance = remainingBalance - thisPaymentPrinciple; // calculate the remaining total balance
taAmmortization.append(i + "\t" + df.format(monthlyPaymentAmt) + "\t\t" + df.format(remainingBalance) + "\t\t" + df.format(thisPaymentInterest) + "\n"); // output the monthly amounts
}
}
//Mortgage Calculation
public void MonthlyPaymentCalculation(){
monthlyPaymentAmt = ((rate / 12) * mortgage) / (1- (Math.pow (1+ (rate / 12), (years * -12))));
}
public static void main(String[] args){
MortgageCalculatorWk4 frame = new MortgageCalculatorWk4();
frame.setTitle("McBride Mortgage Payment Calculator");
frame.setSize(800,600);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}