Results 1 to 8 of 8
- 05-06-2010, 12:10 AM #1
Member
- Join Date
- May 2010
- Posts
- 23
- Rep Power
- 0
Why doesn't my program function correctly?
This is just a simple program that lets users input Principal, Duration and Rate and it will output Compound interest.
It has no errors but it doesn't work, when I put in numbers it just says 0.0 all the time for the result :confused:
Mostly what I have bolded at the bottom is what I need help with!Java Code:public class CompoundInterestGUI extends javax.swing.JFrame { double principal1; double rate1; double duration1; double interest; public CompoundInterestGUI() { initComponents(); } @SuppressWarnings("unchecked") private void initComponents() { principal = new javax.swing.JLabel(); rate = new javax.swing.JLabel(); duration = new javax.swing.JLabel(); principalTextField = new javax.swing.JTextField(); rateTextField = new javax.swing.JTextField(); durationTextField = new javax.swing.JTextField(); result = new javax.swing.JLabel(); jButton1 = new javax.swing.JButton(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); principal.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N principal.setText("Principal:"); rate.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N rate.setText("Rate:"); duration.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N duration.setText("Duration:"); principalTextField.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N principalTextField.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { principalTextFieldActionPerformed(evt); } }); rateTextField.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N rateTextField.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { rateTextFieldActionPerformed(evt); } }); durationTextField.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N durationTextField.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { durationTextFieldActionPerformed(evt); } }); result.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N jButton1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N jButton1.setText("Get Interest!"); jButton1.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); } }); 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() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 238, Short.MAX_VALUE) .addGroup(layout.createSequentialGroup() .addComponent(principal) .addGap(4, 4, 4) .addComponent(principalTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(rate) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(rateTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGroup(layout.createSequentialGroup() .addComponent(duration) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(durationTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 108, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(result, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap()) ); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {duration, principal, rate}); layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {durationTextField, principalTextField, rateTextField}); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false) .addComponent(principalTextField, javax.swing.GroupLayout.Alignment.LEADING) .addComponent(principal, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(rate) .addComponent(rateTextField)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(duration) .addComponent(durationTextField)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(jButton1) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(result, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)) ); pack(); } private void principalTextFieldActionPerformed(java.awt.event.ActionEvent evt) { [b]principal1 = Double.parseDouble(principal.getText()); } private void rateTextFieldActionPerformed(java.awt.event.ActionEvent evt) { rate1 = Double.parseDouble(rate.getText()); } private void durationTextFieldActionPerformed(java.awt.event.ActionEvent evt) { duration1 = Double.parseDouble(duration.getText()); } private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { interest = principal1 * Math.pow(1 + rate1, duration1); result.setText(interest + " $"); [/b] } public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new CompoundInterestGUI().setVisible(true); } }); } private javax.swing.JLabel duration; private javax.swing.JTextField durationTextField; private javax.swing.JButton jButton1; private javax.swing.JLabel principal; private javax.swing.JTextField principalTextField; private javax.swing.JLabel rate; private javax.swing.JTextField rateTextField; private javax.swing.JLabel result; }
- 05-06-2010, 12:16 AM #2
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Lesson 1 in debugging: use the "poor-man's debugger" otherwise known as System.out.println:
Then you'll see that all your variables are 0. Then you have to ask yourself where are you extracting the information from the textfields when the button's pressed. Note that adding action listeners to text fields will have no effect on these variables when the button is pressed, and I'll argue that you should in fact remove all those action listeners from your text fields as they're more liable to cause you problems then to help you.Java Code:private void jButton1ActionPerformed( java.awt.event.ActionEvent evt) { System.out.println("principal1: " + principal1); System.out.println("rate1: " + rate1); System.out.println("duration1: " + duration1); interest = principal1 * Math.pow(1 + rate1, duration1); result.setText(interest + " $"); }
So my suggestions:
1) get rid of all the ActionListeners that you're adding the JTextFields and have only one ActionListener added to the button.
2) In the JButton's ActionListener actionPerformed, extract the numbers from the JTextFields first before you attempt to do any calculations with them.Last edited by curmudgeon; 05-06-2010 at 12:19 AM.
- 05-06-2010, 12:29 AM #3
Member
- Join Date
- May 2010
- Posts
- 23
- Rep Power
- 0
Is this the correct method to get numbers (not strings)?
Java Code:something = Double.parseDouble(otherSomething.getText());
- 05-06-2010, 12:31 AM #4
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
I second what curmudgeon said, and also, you are trying to parseDouble() from your JLabel text, rather than from your JTextField text. That's not going to work out too well for you.
-Gary-
- 05-06-2010, 12:43 AM #5
Member
- Join Date
- May 2010
- Posts
- 23
- Rep Power
- 0
I didn't actually code most of it, I used netbeans GUI builder which essentially lets you drag and drop objects and build the window without coding it. I know probably not the best way and I am in the process of learning to code everything now.
@gcalvin,
How am I parsing from a label? Doesn't this code:
mean I am getting the text from my principal text field and storing it in my principal1 variable?Java Code:private void principalTextFieldActionPerformed(java.awt.event.ActionEvent evt) { principal1 = Double.parseDouble(principal.getText()); }
- 05-06-2010, 12:46 AM #6
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 4
Which is why I recommend you avoid using it. In my opinion it hampers learning too.
YepI know probably not the best way and I am in the process of learning to code everything now.
Check your variable names. What type of variable is "principal"? The answer is up there in your code. What is the name of the corresponding JTextField variable?@gcalvin,
How am I parsing from a label? Doesn't this code:
mean I am getting the text from my principal text field and storing it in my principal1 variable?Java Code:private void principalTextFieldActionPerformed(java.awt.event.ActionEvent evt) { principal1 = Double.parseDouble(principal.getText()); }
- 05-06-2010, 12:53 AM #7
Member
- Join Date
- May 2010
- Posts
- 23
- Rep Power
- 0
Hmm, so the could should be like this then?
At least now it is parsing it from the text field, but it still doesn't work :)Java Code:private void principalTextFieldActionPerformed(java.awt.event.ActionEvent evt) { principle1 = Double.parseDouble(principalTextField.getText()); }
- 05-06-2010, 01:03 AM #8
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
That's closer (there is no principle1 variable -- I think you mean principal1), but do like curmudgeon says and get rid of all those ActionListeners except the JButton one. Do all of your parseDouble()s and everything else right there.
(If you insist on keeping all the other ActionListeners, you should know that they will only be called if you press Enter while your cursor is in that field.)
-Gary-
Similar Threads
-
Did I do this THREAD correctly?
By TimHuey in forum New To JavaReplies: 4Last Post: 04-24-2010, 05:54 AM -
How correctly invoke function "round"?
By artemff in forum New To JavaReplies: 2Last Post: 01-01-2010, 11:31 AM -
Possible? Callback function passed as arguments to another function
By TreyAU21 in forum Advanced JavaReplies: 3Last Post: 12-04-2009, 03:08 PM -
how to scale correctly ?
By h9h in forum Java 2DReplies: 10Last Post: 10-29-2009, 07:06 AM -
If statement not executing correctly
By gligor_kot in forum New To JavaReplies: 5Last Post: 08-03-2009, 01:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks