Results 1 to 8 of 8
  1. #1
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default 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:

    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;                 
    
    }
    Mostly what I have bolded at the bottom is what I need help with!

  2. #2
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Lesson 1 in debugging: use the "poor-man's debugger" otherwise known as System.out.println:

    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 + " $");
      }
    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.

    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 01:19 AM.

  3. #3
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default

    Is this the correct method to get numbers (not strings)?

    Java Code:
    something = Double.parseDouble(otherSomething.getText());

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    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-

  5. #5
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default

    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:
    Java Code:
    private void principalTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
        principal1 = Double.parseDouble(principal.getText());
        }
    mean I am getting the text from my principal text field and storing it in my principal1 variable?

  6. #6
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by robertbob View Post
    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.
    Which is why I recommend you avoid using it. In my opinion it hampers learning too.

    I know probably not the best way and I am in the process of learning to code everything now.
    Yep

    @gcalvin,
    How am I parsing from a label? Doesn't this code:
    Java Code:
    private void principalTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
        principal1 = Double.parseDouble(principal.getText());
        }
    mean I am getting the text from my principal text field and storing it in my principal1 variable?
    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?

  7. #7
    robertbob is offline Member
    Join Date
    May 2010
    Posts
    23
    Rep Power
    0

    Default

    Hmm, so the could should be like this then?

    Java Code:
    private void principalTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
        principle1 = Double.parseDouble(principalTextField.getText());
        }
    At least now it is parsing it from the text field, but it still doesn't work :)

  8. #8
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by robertbob View Post
    Hmm, so the could should be like this then?

    Java Code:
    private void principalTextFieldActionPerformed(java.awt.event.ActionEvent evt) {
        principle1 = Double.parseDouble(principalTextField.getText());
        }
    At least now it is parsing it from the text field, but it still doesn't work :)
    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

  1. Did I do this THREAD correctly?
    By TimHuey in forum New To Java
    Replies: 4
    Last Post: 04-24-2010, 06:54 AM
  2. How correctly invoke function "round"?
    By artemff in forum New To Java
    Replies: 2
    Last Post: 01-01-2010, 12:31 PM
  3. Replies: 3
    Last Post: 12-04-2009, 04:08 PM
  4. how to scale correctly ?
    By h9h in forum Java 2D
    Replies: 10
    Last Post: 10-29-2009, 08:06 AM
  5. If statement not executing correctly
    By gligor_kot in forum New To Java
    Replies: 5
    Last Post: 08-03-2009, 02:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •