Need help on this working properly... it outputs a wrong result.. is it just the math operation at the bottom? What should the equation read as?
private JButton buttonCalc;
public Calculator() {
initComponents();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
pack();
// Add Listeners
buttonCalc.addActionListener(this);
}
public void initComponents() {
//Initialize Components
panelAdder = new JPanel();
labela = new JLabel("Amount");
textFieldAmount = new JTextField();
labelt = new JLabel("Term");
textFieldTerm = new JTextField();
labelr = new JLabel("Rate");
textFieldRate = new JTextField();
textFieldResult = new JTextField();
buttonCalc = new JButton("Calculate");
//Set Object Attributes
textFieldResult.setEditable(false);
textFieldResult.setColumns(8);
textFieldAmount.setColumns(6);
textFieldTerm.setColumns(2);
textFieldRate.setColumns(2);
Container contentPane = getContentPane();
contentPane.setLayout(new FlowLayout());
//Lets add the components to the panel
panelAdder.add(labela);
panelAdder.add(textFieldAmount);
panelAdder.add(labelt);
panelAdder.add(textFieldTerm);
panelAdder.add(labelr);
panelAdder.add(textFieldRate);
panelAdder.add(buttonCalc);
panelAdder.add(textFieldResult);
contentPane.add(panelAdder);
}
public static void main(String [] args) {
Calculator frame = new Calculator();
}
private void setResultValue() {
double amount = Double.parseDouble (textFieldAmount.getText());
double term = Integer.parseInt(textFieldTerm.getText());
double rate = Double.parseDouble(textFieldRate.getText()) / 100.;
double result = amount * ( rate * Math.pow ( ( 1 + rate ), term ) ) / ( Math.pow( ( 1 + rate ), term ) - 1 );
textFieldResult.setText(Double.toString(result));
}
public void actionPerformed(ActionEvent event) {
System.out.println("Action Button");
String command = event.getActionCommand();
if ("Calculate".equals(command)) { setResultValue(); }
}
}
What are you trying to calculate here?
Output the Monthly Mortgage Payment, from what the user inputted for amount, term (years), and rate (interest rate)
Try dividing the rate by 1200 instead of 100
hrm. When I test it with that change, and use 350000 for amount, 30 for term and 7 for rate, the result is 1.16753711824
Shouldn't the term be expressed in months as well, rather than years?
I don't think so. I don't want it to. 12751.16753711824 is definately not a good thing tho. Monthly payment of 12,751 for $350,000 over 30 years on 7 percent interest rate is WAY off to say the least.
What I mean is, you should multiply the term variable by 12 before plugging it into your formula. That 12,000 figure is definitely wrong. It should be around $2300.
If the mentioned changes are applied to now read as:
double term = Integer.parseInt(textFieldTerm.getText()) / 12;
double rate = Double.parseDouble(textFieldRate.getText()) / 1200;
double result = amount * ( rate * Math.pow ( ( 1 + rate ), term ) ) / ( Math.pow( ( 1 + rate ), term ) - 1 );
I get the calculation 176532.73438581926 using the same test input figures as I did to test it before.
So, not working any better I'm afraid.
It should be:
double term = Integer.parseInt(textFieldTerm.getText()) * 12;
Sometimes I'm just not thinking too clearly when it comes to math. Thanks, that's seems to have worked. I got 2328 and change after running it this time. So, now what's left is changing how the result displays, so I don't get all of those extra unneeded numbers. That's easy tho. After that, I'm gonna have to add some more features to it tho, to make it look like a better final project.
I tried get the output of the result to show just two numbers after the decimal, but can't get it to. What do I have to do?
Instead of using:
textFieldResult.setText(Double.toString(result));
in the setResultValue method try the following:
DecimalFormat myFormat = new DecimalFormat("#,###,###,##0.00");
StringBuffer myResult = myFormat.format(result, null, null);
testFieldResult.setText(myResult.toString());
I think this should work.
Calculator.java:72: cannot find symbol
symbol : class DecimalFormat
location: class Calculator
DecimalFormat myFormat = new DecimalFormat("#,###,###,##0.00");
^
Calculator.java:72: cannot find symbol
symbol : class DecimalFormat
location: class Calculator
DecimalFormat myFormat = new DecimalFormat("#,###,###,##0.00");
^
Calculator.java:74: cannot find symbol
symbol : variable testFieldResult
location: class Calculator
testFieldResult.setText(myResult.toString());
^
3 errors
You need to import java.text.DecimalFormat. Also, I made a typo. It should be textFieldResult.
Now this happens after the Calculate button is hit :(
Action Button
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at java.text.DecimalFormat.format(Unknown Source)
at Calculator.setResultValue(Calculator.java:75)
at Calculator.actionPerformed(Calculator.java:85)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Hmm... Looks like I was reading the javadoc wrong. Try this:
DecimalFormat myFormat = new DecimalFormat("#,###,###,##0.00");
textFieldResult.setText(myFormat.format(result));
I even tested this this time, and it worked for me.