ghost8067
asked on
Trouble reading a file with term and rate.
I am having several issues with my mortgage calculator. I am trying to read a file with the following values. I am not sure the file actually will read because I am getting compiler errors.
Any assistance would be apreciated.
Thanks,
Jon
7,5.35
15,5.5
30,5.75
the fiirst value in each row is the term, the second is the yearly interest rate. I added a file reader and now get the following errors.
C:\java\java2\Week 5 program\Mortgage.java:378: operator * cannot be applied to java.lang.String,int
int monthCount = (TermArrayNew[i] * 12);
^
C:\java\java2\Week 5 program\Mortgage.java:384: operator / cannot be applied to java.lang.String,int
interestRateMonthly = (YearlyInterestArrayNew[i] / 12) / 100;
^
C:\java\java2\Week 5 program\Mortgage.java:449: operator < cannot be applied to int,java.lang.String
for (int a = 0; a < TermArrayNew[i]; a = a + 1)
^
Note: C:\java\java2\Week 5 program\Mortgage.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 errors
Tool completed with exit code 1
My code is below
//week3 Jan Schab
/* Mortgage Monyhly Payment Calculator with graphical user interface
Programmer: J.Schab
Date: June 10,2006
Filename: Mortgage.java
Purpose: This project accepts the mortgage and allows the user to select one of 3
pre-determined loans.The program then calculates the monthly mortgage payment and displays
it in currency format. The program then allows the user to display the payment number,
the monthly principle paid, the monthly interest paid, and the current loan balance
through the push of an addition button; There is a quit button to exit the application,
and a clear button to clear user entered fields as well as the amortization table.in
order to calculate a new mortgage. The program will also clear the payment field should
the user press any key while positioned in the input textfield for amount. The payment and
amortizatin table will also be cleared if the user changes the radio buttons.
*/
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JScrollPane.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.text.NumberFormat;
import java.util.Locale;
import java.text.*;
import java.text.DecimalFormat;
import java.io.FileReader;
import java.io.BufferedReader;
import java.text.NumberFormat;
import java.util.ArrayList;
class Mortgage extends JFrame implements ActionListener
{
//Program variables
int payment = 0; // payment counter
int i = 0; // used as index
// initialize variable for monthly interest rate
double monthlyInterest = 0.00;
// variable to store principle
double principle = 0.00;
// initialize mothly principle paid field
double monthlyPrinciplePaid = 0.00;
// Variable to hold maonthly interest
double interestRateMonthly = 0.00;
// variable to store calculated monthly payment
double monthlyPayment;
// variable to store descending balance
int amount = 0;
String YearlyInterestArrayNew[] = null;
String TermArrayNew[] = null;
String InputFile = ".\\TermRate.dat";
private JLabel headerJLabel;
Border rowborder = new EmptyBorder(3, 10, 3, 10);
// Format output
DecimalFormat currency = new DecimalFormat("$0.00");
// JLabel and JTextField for the mortgage amount
private JLabel amountJLabel;
private JTextField amountJTextField;
// JLabel for monthly mortgage payment
private JLabel monthpayJLabel;
// JTextArea for displaying payment
private JTextArea outputJTextArea;
// JButton to initiate calculations
private JButton calculateJButton;
// JButton to display amortization table
private JButton displayJButton;
// JButton to clear entries
private JButton clearJButton;
// JButton to quit
private JButton quitJButton;
// Create radio panel
JPanel radioPanel = new JPanel();
JRadioButton aButton = new JRadioButton("7 Years at 5.35%", true);
JRadioButton bButton = new JRadioButton("15 Years at 5.50%", false);
JRadioButton cButton = new JRadioButton("30 Years at 5.75%", false);
// create scroll box with display area
JTextArea displayArea = new JTextArea(10, 45);
JScrollPane scroll = new JScrollPane(displayArea);
// no-argument constructor
public Mortgage()
{
createUserInterface();
}
// create and position the GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
setTitle("Mortgage Calculator"); // set title bar text
// set northLabel using borderlayout
headerJLabel = new JLabel();
headerJLabel.setBounds(10, 1, 300, 46); // set size and position
headerJLabel.setText("Ente r a mortgage amount and select a loan");
contentPane.add(headerJLab el);
// enable specific positioning of GUI components
contentPane.setLayout(null );
// set up amountJLabel
amountJLabel = new JLabel();
amountJLabel.setBounds(76, 36, 104, 26); // set size and position
amountJLabel.setText("Mort gage amount:");
contentPane.add(amountJLab el);
// set up amountJTextField
amountJTextField = new JTextField();
amountJTextField.setBounds (200, 36, 56, 26); // set size and position
amountJTextField.setHorizo ntalAlignm ent(JTextF ield.RIGHT );
contentPane.add(amountJTex tField);
amountJTextField.addKeyLis tener(
new KeyAdapter() // anonymous inner class
{
// event handler for key pressed in amountJTextField
public void keyPressed(KeyEvent event)
{
amountJTextFieldkeyPressed (event);
}
} // end anonymous inner class
); // end call to addKeyListener
// create radio buttons and panel
ButtonGroup bgroup = new ButtonGroup();
// add listeners for radio buttons
aButton.addActionListener( this);
bButton.addActionListener( this);
cButton.addActionListener( this);
bgroup.add(aButton);
bgroup.add(bButton);
bgroup.add(cButton);
radioPanel.setLayout(new FlowLayout(FlowLayout.LEFT , 4, 4));
radioPanel.add(aButton);
radioPanel.add(bButton);
radioPanel.add(cButton);
contentPane.add(radioPanel );
radioPanel.setBorder(rowbo rder);
radioPanel.setBounds(90, 56, 195, 95); // set size and position
// set up calculateJButton
calculateJButton = new JButton();
calculateJButton.setBounds (26, 155, 90, 26); // set size and position
calculateJButton.setText(" Calculate" );
contentPane.add(calculateJ Button);
calculateJButton.addAction Listener(
new ActionListener() // anonymous inner class
{
// event handler called when calculateJButton is pressed
public void actionPerformed(ActionEven t event)
{
calculateJButtonActionPerf ormed(even t);
}
} // end anonymous inner class
); // end call to addActionListener
// set up displayJButton
displayJButton = new JButton();
displayJButton.setBounds(2 6, 216, 200, 26); // set size and position
displayJButton.setText("Di splay Amortization Table");
contentPane.add(displayJBu tton);
displayJButton.addActionLi stener(
new ActionListener() // anonymous inner class
{
// event handler called when clearJButton is pressed
public void actionPerformed(ActionEven t event)
{
displayJButtonActionPerfor med(event) ;
}
} // end anonymous inner class
); // end call to addActionListener
// set up clearJButton
clearJButton = new JButton();
clearJButton.setBounds(70, 380, 90, 26); // set size and position
clearJButton.setText("Clea r");
contentPane.add(clearJButt on);
clearJButton.addActionList ener(
new ActionListener() // anonymous inner class
{
// event handler called when clearJButton is pressed
public void actionPerformed(ActionEven t event)
{
clearJButtonActionPerforme d(event);
}
} // end anonymous inner class
); // end call to addActionListener
// set up quitJButton
quitJButton = new JButton();
quitJButton.setBounds(230, 380, 90, 26); // set size and position
quitJButton.setText("Quit" );
contentPane.add(quitJButto n);
quitJButton.addActionListe ner(
new ActionListener() // anonymous inner class
{
// event handler called when quitJButton is pressed
public void actionPerformed(ActionEven t event)
{
quitJButtonActionPerformed (event);
}
} // end anonymous inner class
); // end call to addActionListener
// set up outputJTextArea
outputJTextArea = new JTextArea();
contentPane.add(outputJTex tArea);
outputJTextArea.setBounds( 300, 155, 56, 26); // set size and position
outputJTextArea.setEditabl e(false);
//set up monthpayJLabel
monthpayJLabel = new JLabel();
monthpayJLabel.setBounds(1 76, 155, 156, 26); // set size and position
monthpayJLabel.setText("Mo nthly Payment:");
contentPane.add(monthpayJL abel);
scroll.setBorder(BorderFac tory.creat eEmptyBord er(10, 10, 10, 10));
contentPane.add(scroll);
scroll.setBounds(20, 240, 369, 130); // set size and position
setVisible(true);
displayArea.setEditable(fa lse);
FileReader InputFile;
try {
InputFile = new FileReader("TermRate.dat") ;
BufferedReader data = new BufferedReader(InputFile);
String aline = null;
java.util.ArrayList array = new java.util.ArrayList();
while ((aline = data.readLine()) != null)
array.add(aline);
YearlyInterestArrayNew = new String[array.size()];
TermArrayNew = new String[array.size()];
for (int i = 0; i < array.size(); i++) {
String[] line = ((String) array.get(i)).split(",");
YearlyInterestArrayNew[i] = line[0].trim();
TermArrayNew[i] = line[1].trim();
}
InputFile.close();
} catch (Exception e1) {
e1.printStackTrace();
}
//MyComboBox();
//}
//double Term = Double.parseDouble((String )TermArray New.get(i) );
//double Interest = Double.parseDouble((String )YearlyInt erestArray New.get(i) );
} // end method createUserInterface
// called when user presses key in amountJTextField
private void amountJTextFieldkeyPressed (KeyEvent event)
{
outputJTextArea.setText("" ); // clear outputJTextArea
displayArea.setText(""); // clear display
payment = 0; // reset payment counter
}
// method called when user clicks calculateJButton
private void calculateJButtonActionPerf ormed(Acti onEvent event)
{
try
{
// check which radio buttonis selected and set index
if (aButton.isSelected())
{
i = 0;
}
if (bButton.isSelected())
{
i = 1;
}
if (cButton.isSelected())
{
i = 2;
}
//define display area titles
String titles = "Month \t Principal\t Interest\tBalance\n";
//put titles in display area
displayArea.setText(titles );
// initialize loan balance field
double loanbalance = 0.00;
// initialize payment counter
// clear outputJTextArea
outputJTextArea.setText("" );
// clear display area
displayArea.setText("");
// Calculate months from years
int monthCount = (TermArrayNew[i] * 12);
// get mortgage amount
amount = Integer.parseInt(amountJTe xtField.ge tText());
//create variable to store decreasing principle
principle = amount;
// Calculate monthly interest
interestRateMonthly = (YearlyInterestArrayNew[i] / 12) / 100;
// Calculate monthly payment
monthlyPayment = (amount * interestRateMonthly) / (1-Math.pow(1
+interestRateMonthly, - monthCount));
// Output payment amount
outputJTextArea.setText(cu rrency.for mat(monthl yPayment)) ;
}
catch (NumberFormatException ex){}
}
// method called when user clicks clearJButton
private void clearJButtonActionPerforme d(ActionEv ent event)
{
try
{
monthlyPrinciplePaid = 0.00; // initialize principle paid field
outputJTextArea.setText("" ); // clear outputJTextArea
amountJTextField.setText(" "); // clear amountJTextField
displayArea.setText(""); // clear display area
payment = 0; // reset payment counter
}
catch (NumberFormatException ex){}
}
// method called when user clicks displayJButton
private void displayJButtonActionPerfor med(Action Event event)
{
try
{
// reset values in case user presses display more than once
principle = Integer.parseInt(amountJTe xtField.ge tText());
monthlyPrinciplePaid = 0.00;
displayArea.setText(""); // clear display
payment = 0;
monthlyPrinciplePaid = 0.00;
//define display area titles
String titles = "Month \t Principal\t Interest\tBalance\n";
//put titles in display area
displayArea.setText(titles );
// initialize loan balance field
// loop for creating amortization table
for (int a = 0; a < TermArrayNew[i]; a = a + 1)
{
//increment payment counter
payment++;
// calculate monthly interest
monthlyInterest = principle * interestRateMonthly;
// calculate monthly principle
monthlyPrinciplePaid = monthlyPayment - monthlyInterest; //
//save declining balance
principle = principle - monthlyPrinciplePaid;
// display amortization table
displayArea.append(payment + "\t" + currency.format
(monthlyPrinciplePaid) + "\t" + currency.format
(monthlyInterest) + "\t" + currency.format(Math.abs
(principle)) + "\n");
displayArea.setCaretPositi on(0);
}
}
catch (NumberFormatException ex){}
}
// method called when user clicks quitJButton
private void quitJButtonActionPerformed (ActionEve nt event)
{
try
{
System.exit(0);
}
catch (NumberFormatException ex){}
}
// method called when user clicks on a radio button
public void actionPerformed(ActionEven t e)
{
displayArea.setText(""); // clear display
outputJTextArea.setText("" ); // clear outputJTextArea
}
// main method
public static void main(String[]args)
{
Mortgage application = new Mortgage();
application.setDefaultClos eOperation (JFrame.EX IT_ON_CLOS E);
application.setSize(400, 450);
application.setVisible(tru e);
} // end method main
} // end class Mortgage
Any assistance would be apreciated.
Thanks,
Jon
7,5.35
15,5.5
30,5.75
the fiirst value in each row is the term, the second is the yearly interest rate. I added a file reader and now get the following errors.
C:\java\java2\Week 5 program\Mortgage.java:378:
int monthCount = (TermArrayNew[i] * 12);
^
C:\java\java2\Week 5 program\Mortgage.java:384:
interestRateMonthly = (YearlyInterestArrayNew[i]
^
C:\java\java2\Week 5 program\Mortgage.java:449:
for (int a = 0; a < TermArrayNew[i]; a = a + 1)
^
Note: C:\java\java2\Week 5 program\Mortgage.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
3 errors
Tool completed with exit code 1
My code is below
//week3 Jan Schab
/* Mortgage Monyhly Payment Calculator with graphical user interface
Programmer: J.Schab
Date: June 10,2006
Filename: Mortgage.java
Purpose: This project accepts the mortgage and allows the user to select one of 3
pre-determined loans.The program then calculates the monthly mortgage payment and displays
it in currency format. The program then allows the user to display the payment number,
the monthly principle paid, the monthly interest paid, and the current loan balance
through the push of an addition button; There is a quit button to exit the application,
and a clear button to clear user entered fields as well as the amortization table.in
order to calculate a new mortgage. The program will also clear the payment field should
the user press any key while positioned in the input textfield for amount. The payment and
amortizatin table will also be cleared if the user changes the radio buttons.
*/
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JScrollPane.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.text.NumberFormat;
import java.util.Locale;
import java.text.*;
import java.text.DecimalFormat;
import java.io.FileReader;
import java.io.BufferedReader;
import java.text.NumberFormat;
import java.util.ArrayList;
class Mortgage extends JFrame implements ActionListener
{
//Program variables
int payment = 0; // payment counter
int i = 0; // used as index
// initialize variable for monthly interest rate
double monthlyInterest = 0.00;
// variable to store principle
double principle = 0.00;
// initialize mothly principle paid field
double monthlyPrinciplePaid = 0.00;
// Variable to hold maonthly interest
double interestRateMonthly = 0.00;
// variable to store calculated monthly payment
double monthlyPayment;
// variable to store descending balance
int amount = 0;
String YearlyInterestArrayNew[] = null;
String TermArrayNew[] = null;
String InputFile = ".\\TermRate.dat";
private JLabel headerJLabel;
Border rowborder = new EmptyBorder(3, 10, 3, 10);
// Format output
DecimalFormat currency = new DecimalFormat("$0.00");
// JLabel and JTextField for the mortgage amount
private JLabel amountJLabel;
private JTextField amountJTextField;
// JLabel for monthly mortgage payment
private JLabel monthpayJLabel;
// JTextArea for displaying payment
private JTextArea outputJTextArea;
// JButton to initiate calculations
private JButton calculateJButton;
// JButton to display amortization table
private JButton displayJButton;
// JButton to clear entries
private JButton clearJButton;
// JButton to quit
private JButton quitJButton;
// Create radio panel
JPanel radioPanel = new JPanel();
JRadioButton aButton = new JRadioButton("7 Years at 5.35%", true);
JRadioButton bButton = new JRadioButton("15 Years at 5.50%", false);
JRadioButton cButton = new JRadioButton("30 Years at 5.75%", false);
// create scroll box with display area
JTextArea displayArea = new JTextArea(10, 45);
JScrollPane scroll = new JScrollPane(displayArea);
// no-argument constructor
public Mortgage()
{
createUserInterface();
}
// create and position the GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
setTitle("Mortgage Calculator"); // set title bar text
// set northLabel using borderlayout
headerJLabel = new JLabel();
headerJLabel.setBounds(10,
headerJLabel.setText("Ente
contentPane.add(headerJLab
// enable specific positioning of GUI components
contentPane.setLayout(null
// set up amountJLabel
amountJLabel = new JLabel();
amountJLabel.setBounds(76,
amountJLabel.setText("Mort
contentPane.add(amountJLab
// set up amountJTextField
amountJTextField = new JTextField();
amountJTextField.setBounds
amountJTextField.setHorizo
contentPane.add(amountJTex
amountJTextField.addKeyLis
new KeyAdapter() // anonymous inner class
{
// event handler for key pressed in amountJTextField
public void keyPressed(KeyEvent event)
{
amountJTextFieldkeyPressed
}
} // end anonymous inner class
); // end call to addKeyListener
// create radio buttons and panel
ButtonGroup bgroup = new ButtonGroup();
// add listeners for radio buttons
aButton.addActionListener(
bButton.addActionListener(
cButton.addActionListener(
bgroup.add(aButton);
bgroup.add(bButton);
bgroup.add(cButton);
radioPanel.setLayout(new FlowLayout(FlowLayout.LEFT
radioPanel.add(aButton);
radioPanel.add(bButton);
radioPanel.add(cButton);
contentPane.add(radioPanel
radioPanel.setBorder(rowbo
radioPanel.setBounds(90, 56, 195, 95); // set size and position
// set up calculateJButton
calculateJButton = new JButton();
calculateJButton.setBounds
calculateJButton.setText("
contentPane.add(calculateJ
calculateJButton.addAction
new ActionListener() // anonymous inner class
{
// event handler called when calculateJButton is pressed
public void actionPerformed(ActionEven
{
calculateJButtonActionPerf
}
} // end anonymous inner class
); // end call to addActionListener
// set up displayJButton
displayJButton = new JButton();
displayJButton.setBounds(2
displayJButton.setText("Di
contentPane.add(displayJBu
displayJButton.addActionLi
new ActionListener() // anonymous inner class
{
// event handler called when clearJButton is pressed
public void actionPerformed(ActionEven
{
displayJButtonActionPerfor
}
} // end anonymous inner class
); // end call to addActionListener
// set up clearJButton
clearJButton = new JButton();
clearJButton.setBounds(70,
clearJButton.setText("Clea
contentPane.add(clearJButt
clearJButton.addActionList
new ActionListener() // anonymous inner class
{
// event handler called when clearJButton is pressed
public void actionPerformed(ActionEven
{
clearJButtonActionPerforme
}
} // end anonymous inner class
); // end call to addActionListener
// set up quitJButton
quitJButton = new JButton();
quitJButton.setBounds(230,
quitJButton.setText("Quit"
contentPane.add(quitJButto
quitJButton.addActionListe
new ActionListener() // anonymous inner class
{
// event handler called when quitJButton is pressed
public void actionPerformed(ActionEven
{
quitJButtonActionPerformed
}
} // end anonymous inner class
); // end call to addActionListener
// set up outputJTextArea
outputJTextArea = new JTextArea();
contentPane.add(outputJTex
outputJTextArea.setBounds(
outputJTextArea.setEditabl
//set up monthpayJLabel
monthpayJLabel = new JLabel();
monthpayJLabel.setBounds(1
monthpayJLabel.setText("Mo
contentPane.add(monthpayJL
scroll.setBorder(BorderFac
contentPane.add(scroll);
scroll.setBounds(20, 240, 369, 130); // set size and position
setVisible(true);
displayArea.setEditable(fa
FileReader InputFile;
try {
InputFile = new FileReader("TermRate.dat")
BufferedReader data = new BufferedReader(InputFile);
String aline = null;
java.util.ArrayList array = new java.util.ArrayList();
while ((aline = data.readLine()) != null)
array.add(aline);
YearlyInterestArrayNew = new String[array.size()];
TermArrayNew = new String[array.size()];
for (int i = 0; i < array.size(); i++) {
String[] line = ((String) array.get(i)).split(",");
YearlyInterestArrayNew[i] = line[0].trim();
TermArrayNew[i] = line[1].trim();
}
InputFile.close();
} catch (Exception e1) {
e1.printStackTrace();
}
//MyComboBox();
//}
//double Term = Double.parseDouble((String
//double Interest = Double.parseDouble((String
} // end method createUserInterface
// called when user presses key in amountJTextField
private void amountJTextFieldkeyPressed
{
outputJTextArea.setText(""
displayArea.setText(""); // clear display
payment = 0; // reset payment counter
}
// method called when user clicks calculateJButton
private void calculateJButtonActionPerf
{
try
{
// check which radio buttonis selected and set index
if (aButton.isSelected())
{
i = 0;
}
if (bButton.isSelected())
{
i = 1;
}
if (cButton.isSelected())
{
i = 2;
}
//define display area titles
String titles = "Month \t Principal\t Interest\tBalance\n";
//put titles in display area
displayArea.setText(titles
// initialize loan balance field
double loanbalance = 0.00;
// initialize payment counter
// clear outputJTextArea
outputJTextArea.setText(""
// clear display area
displayArea.setText("");
// Calculate months from years
int monthCount = (TermArrayNew[i] * 12);
// get mortgage amount
amount = Integer.parseInt(amountJTe
//create variable to store decreasing principle
principle = amount;
// Calculate monthly interest
interestRateMonthly = (YearlyInterestArrayNew[i]
// Calculate monthly payment
monthlyPayment = (amount * interestRateMonthly) / (1-Math.pow(1
+interestRateMonthly, - monthCount));
// Output payment amount
outputJTextArea.setText(cu
}
catch (NumberFormatException ex){}
}
// method called when user clicks clearJButton
private void clearJButtonActionPerforme
{
try
{
monthlyPrinciplePaid = 0.00; // initialize principle paid field
outputJTextArea.setText(""
amountJTextField.setText("
displayArea.setText(""); // clear display area
payment = 0; // reset payment counter
}
catch (NumberFormatException ex){}
}
// method called when user clicks displayJButton
private void displayJButtonActionPerfor
{
try
{
// reset values in case user presses display more than once
principle = Integer.parseInt(amountJTe
monthlyPrinciplePaid = 0.00;
displayArea.setText(""); // clear display
payment = 0;
monthlyPrinciplePaid = 0.00;
//define display area titles
String titles = "Month \t Principal\t Interest\tBalance\n";
//put titles in display area
displayArea.setText(titles
// initialize loan balance field
// loop for creating amortization table
for (int a = 0; a < TermArrayNew[i]; a = a + 1)
{
//increment payment counter
payment++;
// calculate monthly interest
monthlyInterest = principle * interestRateMonthly;
// calculate monthly principle
monthlyPrinciplePaid = monthlyPayment - monthlyInterest; //
//save declining balance
principle = principle - monthlyPrinciplePaid;
// display amortization table
displayArea.append(payment
(monthlyPrinciplePaid) + "\t" + currency.format
(monthlyInterest) + "\t" + currency.format(Math.abs
(principle)) + "\n");
displayArea.setCaretPositi
}
}
catch (NumberFormatException ex){}
}
// method called when user clicks quitJButton
private void quitJButtonActionPerformed
{
try
{
System.exit(0);
}
catch (NumberFormatException ex){}
}
// method called when user clicks on a radio button
public void actionPerformed(ActionEven
{
displayArea.setText(""); // clear display
outputJTextArea.setText(""
}
// main method
public static void main(String[]args)
{
Mortgage application = new Mortgage();
application.setDefaultClos
application.setSize(400, 450);
application.setVisible(tru
} // end method main
} // end class Mortgage
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It compiles now but does not give any value for the result. It also gives me the following warning. Any idea what this is?
Note: C:\java\java2\Week 5 program\Mortgage.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Tool completed successfully
I couldn't get it to work with the calculation below.
interestRateMonthly = (YearlyInterestArrayNew[i] / 12) / 100;
I created an interim variable as a douple (double calcValue = Double.parseDouble(TermArr ayNew[i]); ) and then did the calculation. I am not sure if this will hurt the calculation.
interestRateMonthly = (calcValue / 12) / 100;
Your answer worked for that issue, so I am accepting it. I was also wondering if you can see if I am doing something wrong when populating the array from the file since I get no result.
Thanks,
Jon
Note: C:\java\java2\Week 5 program\Mortgage.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Tool completed successfully
I couldn't get it to work with the calculation below.
interestRateMonthly = (YearlyInterestArrayNew[i]
I created an interim variable as a douple (double calcValue = Double.parseDouble(TermArr
interestRateMonthly = (calcValue / 12) / 100;
Your answer worked for that issue, so I am accepting it. I was also wondering if you can see if I am doing something wrong when populating the array from the file since I get no result.
Thanks,
Jon
ghost8067, can you tell me why you accepted answer when i'd already told you that?
ASKER
I apoligize. That was my mistake. I am a relative newbie at this, and did not notice that it was a different person that left the update.I should have looked more carefully. I thought it was you. I now can see it was a different responder. Is there a way I can give points to you also?
I am sorry, the points should have gone to you.
Jon
I am sorry, the points should have gone to you.
Jon
OK don't worry - i'll get the question reopened.
A split is fine with me :)
ASKER
Sonds good. One more question before I screw it up.
How do I split the points?
Jon
How do I split the points?
Jon
use the split points link below the last comment
on your other q
instead of
interestRateMonthly = (calcValue / 12) / 100;
try:
interestRateMonthly = (calcValue / 12.0) / 100.0;
instead of
interestRateMonthly = (calcValue / 12) / 100;
try:
interestRateMonthly = (calcValue / 12.0) / 100.0;
:-)
ASKER
The new error is
C:\java\java2\Week 5 program\Mortgage.java:385:
int monthCount = Integer.parseInt(TermArray
Code below.
Jon
//week3 Jan Schab
/* Mortgage Monyhly Payment Calculator with graphical user interface
Programmer: J.Schab
Date: June 10,2006
Filename: Mortgage.java
Purpose: This project accepts the mortgage and allows the user to select one of 3
pre-determined loans.The program then calculates the monthly mortgage payment and displays
it in currency format. The program then allows the user to display the payment number,
the monthly principle paid, the monthly interest paid, and the current loan balance
through the push of an addition button; There is a quit button to exit the application,
and a clear button to clear user entered fields as well as the amortization table.in
order to calculate a new mortgage. The program will also clear the payment field should
the user press any key while positioned in the input textfield for amount. The payment and
amortizatin table will also be cleared if the user changes the radio buttons.
*/
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JScrollPane.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.text.NumberFormat;
import java.util.Locale;
import java.text.*;
import java.text.DecimalFormat;
import java.io.FileReader;
import java.io.BufferedReader;
import java.text.NumberFormat;
import java.util.ArrayList;
class Mortgage extends JFrame implements ActionListener
{
//Program variables
int payment = 0; // payment counter
int i = 0; // used as index
// initialize variable for monthly interest rate
double monthlyInterest = 0.00;
// variable to store principle
double principle = 0.00;
// initialize mothly principle paid field
double monthlyPrinciplePaid = 0.00;
// Variable to hold maonthly interest
double interestRateMonthly = 0.00;
// variable to store calculated monthly payment
double monthlyPayment;
// variable to store descending balance
int amount = 0;
String YearlyInterestArrayNew[] = null;
String TermArrayNew[] = null;
String InputFile = ".\\TermRate.dat";
private JLabel headerJLabel;
Border rowborder = new EmptyBorder(3, 10, 3, 10);
// Format output
DecimalFormat currency = new DecimalFormat("$0.00");
// JLabel and JTextField for the mortgage amount
private JLabel amountJLabel;
private JTextField amountJTextField;
// JLabel for monthly mortgage payment
private JLabel monthpayJLabel;
// JTextArea for displaying payment
private JTextArea outputJTextArea;
// JButton to initiate calculations
private JButton calculateJButton;
// JButton to display amortization table
private JButton displayJButton;
// JButton to clear entries
private JButton clearJButton;
// JButton to quit
private JButton quitJButton;
// Create radio panel
JPanel radioPanel = new JPanel();
JRadioButton aButton = new JRadioButton("7 Years at 5.35%", true);
JRadioButton bButton = new JRadioButton("15 Years at 5.50%", false);
JRadioButton cButton = new JRadioButton("30 Years at 5.75%", false);
// create scroll box with display area
JTextArea displayArea = new JTextArea(10, 45);
JScrollPane scroll = new JScrollPane(displayArea);
// no-argument constructor
public Mortgage()
{
createUserInterface();
}
// create and position the GUI components; register event handlers
private void createUserInterface()
{
// get content pane for attaching GUI components
Container contentPane = getContentPane();
setTitle("Mortgage Calculator"); // set title bar text
// set northLabel using borderlayout
headerJLabel = new JLabel();
headerJLabel.setBounds(10,
headerJLabel.setText("Ente
contentPane.add(headerJLab
// enable specific positioning of GUI components
contentPane.setLayout(null
// set up amountJLabel
amountJLabel = new JLabel();
amountJLabel.setBounds(76,
amountJLabel.setText("Mort
contentPane.add(amountJLab
// set up amountJTextField
amountJTextField = new JTextField();
amountJTextField.setBounds
amountJTextField.setHorizo
contentPane.add(amountJTex
amountJTextField.addKeyLis
new KeyAdapter() // anonymous inner class
{
// event handler for key pressed in amountJTextField
public void keyPressed(KeyEvent event)
{
amountJTextFieldkeyPressed
}
} // end anonymous inner class
); // end call to addKeyListener
// create radio buttons and panel
ButtonGroup bgroup = new ButtonGroup();
// add listeners for radio buttons
aButton.addActionListener(
bButton.addActionListener(
cButton.addActionListener(
bgroup.add(aButton);
bgroup.add(bButton);
bgroup.add(cButton);
radioPanel.setLayout(new FlowLayout(FlowLayout.LEFT
radioPanel.add(aButton);
radioPanel.add(bButton);
radioPanel.add(cButton);
contentPane.add(radioPanel
radioPanel.setBorder(rowbo
radioPanel.setBounds(90, 56, 195, 95); // set size and position
// set up calculateJButton
calculateJButton = new JButton();
calculateJButton.setBounds
calculateJButton.setText("
contentPane.add(calculateJ
calculateJButton.addAction
new ActionListener() // anonymous inner class
{
// event handler called when calculateJButton is pressed
public void actionPerformed(ActionEven
{
calculateJButtonActionPerf
}
} // end anonymous inner class
); // end call to addActionListener
// set up displayJButton
displayJButton = new JButton();
displayJButton.setBounds(2
displayJButton.setText("Di
contentPane.add(displayJBu
displayJButton.addActionLi
new ActionListener() // anonymous inner class
{
// event handler called when clearJButton is pressed
public void actionPerformed(ActionEven
{
displayJButtonActionPerfor
}
} // end anonymous inner class
); // end call to addActionListener
// set up clearJButton
clearJButton = new JButton();
clearJButton.setBounds(70,
clearJButton.setText("Clea
contentPane.add(clearJButt
clearJButton.addActionList
new ActionListener() // anonymous inner class
{
// event handler called when clearJButton is pressed
public void actionPerformed(ActionEven
{
clearJButtonActionPerforme
}
} // end anonymous inner class
); // end call to addActionListener
// set up quitJButton
quitJButton = new JButton();
quitJButton.setBounds(230,
quitJButton.setText("Quit"
contentPane.add(quitJButto
quitJButton.addActionListe
new ActionListener() // anonymous inner class
{
// event handler called when quitJButton is pressed
public void actionPerformed(ActionEven
{
quitJButtonActionPerformed
}
} // end anonymous inner class
); // end call to addActionListener
// set up outputJTextArea
outputJTextArea = new JTextArea();
contentPane.add(outputJTex
outputJTextArea.setBounds(
outputJTextArea.setEditabl
//set up monthpayJLabel
monthpayJLabel = new JLabel();
monthpayJLabel.setBounds(1
monthpayJLabel.setText("Mo
contentPane.add(monthpayJL
scroll.setBorder(BorderFac
contentPane.add(scroll);
scroll.setBounds(20, 240, 369, 130); // set size and position
setVisible(true);
displayArea.setEditable(fa
FileReader InputFile;
try {
InputFile = new FileReader("TermRate.dat")
BufferedReader data = new BufferedReader(InputFile);
String aline = null;
java.util.ArrayList array = new java.util.ArrayList();
while ((aline = data.readLine()) != null)
array.add(aline);
YearlyInterestArrayNew = new String[array.size()];
TermArrayNew = new String[array.size()];
for (int i = 0; i < array.size(); i++) {
String[] line = ((String) array.get(i)).split(",");
YearlyInterestArrayNew[i] = line[0].trim();
TermArrayNew[i] = line[1].trim();
}
InputFile.close();
} catch (Exception e1) {
e1.printStackTrace();
}
//MyComboBox();
//}
//double Term = Double.parseDouble((String
//double Interest = Double.parseDouble((String
} // end method createUserInterface
// called when user presses key in amountJTextField
private void amountJTextFieldkeyPressed
{
outputJTextArea.setText(""
displayArea.setText(""); // clear display
payment = 0; // reset payment counter
}
// method called when user clicks calculateJButton
private void calculateJButtonActionPerf
{
try
{
// check which radio buttonis selected and set index
if (aButton.isSelected())
{
i = 0;
}
if (bButton.isSelected())
{
i = 1;
}
if (cButton.isSelected())
{
i = 2;
}
//define display area titles
String titles = "Month \t Principal\t Interest\tBalance\n";
//put titles in display area
displayArea.setText(titles
// initialize loan balance field
double loanbalance = 0.00;
// initialize payment counter
// clear outputJTextArea
outputJTextArea.setText(""
// clear display area
displayArea.setText("");
// Calculate months from years
//Integer.parseInt(TermArr
//Integer.parseInt(YearlyI
int monthCount = Integer.parseInt(TermArray
// get mortgage amount
amount = Integer.parseInt(amountJTe
//create variable to store decreasing principle
principle = amount;
// Calculate monthly interest
interestRateMonthly = (YearlyInterestArrayNew[i]
// Calculate monthly payment
monthlyPayment = (amount * interestRateMonthly) / (1-Math.pow(1
+interestRateMonthly, - monthCount));
// Output payment amount
outputJTextArea.setText(cu
}
catch (NumberFormatException ex){}
}
// method called when user clicks clearJButton
private void clearJButtonActionPerforme
{
try
{
monthlyPrinciplePaid = 0.00; // initialize principle paid field
outputJTextArea.setText(""
amountJTextField.setText("
displayArea.setText(""); // clear display area
payment = 0; // reset payment counter
}
catch (NumberFormatException ex){}
}
// method called when user clicks displayJButton
private void displayJButtonActionPerfor
{
try
{
// reset values in case user presses display more than once
principle = Integer.parseInt(amountJTe
monthlyPrinciplePaid = 0.00;
displayArea.setText(""); // clear display
payment = 0;
monthlyPrinciplePaid = 0.00;
//define display area titles
String titles = "Month \t Principal\t Interest\tBalance\n";
//put titles in display area
displayArea.setText(titles
// initialize loan balance field
// loop for creating amortization table
for (int a = 0; a < TermArrayNew[i]; a = a + 1)
{
//increment payment counter
payment++;
// calculate monthly interest
monthlyInterest = principle * interestRateMonthly;
// calculate monthly principle
monthlyPrinciplePaid = monthlyPayment - monthlyInterest; //
//save declining balance
principle = principle - monthlyPrinciplePaid;
// display amortization table
displayArea.append(payment
(monthlyPrinciplePaid) + "\t" + currency.format
(monthlyInterest) + "\t" + currency.format(Math.abs
(principle)) + "\n");
displayArea.setCaretPositi
}
}
catch (NumberFormatException ex){}
}
// method called when user clicks quitJButton
private void quitJButtonActionPerformed
{
try
{
System.exit(0);
}
catch (NumberFormatException ex){}
}
// method called when user clicks on a radio button
public void actionPerformed(ActionEven
{
displayArea.setText(""); // clear display
outputJTextArea.setText(""
}
// main method
public static void main(String[]args)
{
Mortgage application = new Mortgage();
application.setDefaultClos
application.setSize(400, 450);
application.setVisible(tru
} // end method main
} // end class Mortgage