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.