Java Code error

Need help with code within GUI code. Math error (NaN)

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1308 Views - Last Post: 19 May 2010 - 11:49 PM Rate Topic: -----

#1 em_123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-May 10

Java Code error

Posted 18 May 2010 - 04:43 PM

I have been set an ssignment where I need to have a form that calculates Miles Per Gallon. I have done this bit, but now I have to have a button that calcultes the average Miles per gallon from n inputs.

I have tried to do a loop, but it either outputs the same number, e.g. hasn't added the previous input. Or it now comes out with NaN. (The average output is sent to jTextField4).

Please help!!

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        float total = 0;
        float i;
        float num3 = 0;
        float averageMpg;

        num3 = Float.parseFloat(jTextField3.getText());

        for (i=0; i<=100; i++) {
                        
            total = total + num3;
        }
            
            


averageMpg = total/(i-1);  // i is 4 after the loop has finished incrementing
jTextField4.setText(String.valueOf(averageMpg));



I also have this code that did the same thing:

        float num3;
        float total = 0;
        int count = 0;
        float averageMpg = 0;

        num3 = Float.parseFloat(jTextField3.getText());
       


        if (total < 0){

                total+=num3;
                count++;
            }

            averageMpg = total/count;

            jTextField4.setText(String.valueOf(averageMpg));



Edited by macosxnerd101: Welcome to DIC! :) Please remember to post your code using code tags, like so: :code:

This post has been edited by macosxnerd101: 18 May 2010 - 05:53 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Java Code error

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10438
  • View blog
  • Posts: 38,657
  • Joined: 27-December 08

Re: Java Code error

Posted 18 May 2010 - 05:57 PM

In reading your comments, you have a misconception that i = 4 after the loop. Actually, i = 101 after the loop. So if num3 = 10, 101 * 10 = 1010/100 = 10.1, which is the answer I got when I ran your code. Can you provide some inputs for each variable, as well as the expected and observed output for that set of values?
Was This Post Helpful? 0
  • +
  • -

#3 em_123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-May 10

Re: Java Code error

Posted 19 May 2010 - 03:38 AM

View Postem_123, on 18 May 2010 - 03:43 PM, said:

I have been set an ssignment where I need to have a form that calculates Miles Per Gallon. I have done this bit, but now I have to have a button that calcultes the average Miles per gallon from n inputs.

I have tried to do a loop, but it either outputs the same number, e.g. hasn't added the previous input. Or it now comes out with NaN. (The average output is sent to jTextField4).

Please help!!

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        float total = 0;
        float i;
        float num3 = 0;
        float averageMpg;

        num3 = Float.parseFloat(jTextField3.getText());

        for (i=0; i<=100; i++) {
                        
            total = total + num3;
        }
            
            


averageMpg = total/(i-1);  // i is 4 after the loop has finished incrementing
jTextField4.setText(String.valueOf(averageMpg));



I also have this code that did the same thing:

        float num3;
        float total = 0;
        int count = 0;
        float averageMpg = 0;

        num3 = Float.parseFloat(jTextField3.getText());
       


        if (total < 0){

                total+=num3;
                count++;
            }

            averageMpg = total/count;

            jTextField4.setText(String.valueOf(averageMpg));



Edited by macosxnerd101: Welcome to DIC! :) Please remember to post your code using code tags, like so: :code:

Was This Post Helpful? 0
  • +
  • -

#4 em_123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-May 10

Re: Java Code error

Posted 19 May 2010 - 03:48 AM

That's thing see, I have a GUI. I have two boxes (jTextField1 and jTextField2). These are then calculated via a button (to get the MilesPerGallon), and the result is sent to jTextField3.

Now, i need the program to remember that number everytime a different number has been calculated. I thought by putting in a loop, it will take the number and keep on adding it. But I think what I have done is add the position number.

Example of input by user:

Miles: 320 fuel consumption: 18.5

calculated result(automatic):17.297297.

After this number is calculated, i then go on to add another mileage and fuel input and calcualte it. For example,

miles: 123 fuel consumption: 15

calculated result(automatic): 8.2.

What I need my button (previous code) to do is take the remember the number every time a number appears in jTextField3 (or num3 as I have called it in the code), add them all up, for example,

17.29729 + 8.2 + (any other results I may have calculated) = total.

(This is what I thought total+=num3 was)

I then need to calculate the average for this new result by the count - the number of values inputted.

I hope this exlpains myself better, please help!
Was This Post Helpful? 0
  • +
  • -

#5 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 292
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: Java Code error

Posted 19 May 2010 - 07:16 AM

What you could do is set up a double array of significant size then simply store the value of JTextField3 into the 0th position of the array. You could then increment an int to keep track of what index you are at. Do you understand?
Was This Post Helpful? 0
  • +
  • -

#6 em_123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-May 10

Re: Java Code error

Posted 19 May 2010 - 11:35 AM

View PostLuckless, on 19 May 2010 - 06:16 AM, said:

What you could do is set up a double array of significant size then simply store the value of JTextField3 into the 0th position of the array. You could then increment an int to keep track of what index you are at. Do you understand?


I'm not sure how I would go about that?? could you give me some pointers?
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10438
  • View blog
  • Posts: 38,657
  • Joined: 27-December 08

Re: Java Code error

Posted 19 May 2010 - 12:05 PM

An easier idea would be to store double variables for totalMiles and totalGallons. Then just divide when you go to calculate the average.
Was This Post Helpful? 1
  • +
  • -

#8 em_123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-May 10

Re: Java Code error

Posted 19 May 2010 - 12:08 PM

View Postmacosxnerd101, on 19 May 2010 - 11:05 AM, said:

An easier idea would be to store double variables for totalMiles and totalGallons. Then just divide when you go to calculate the average.


I did try that, but that's my problem, I can't understand how to make a code that would add total number of miles??
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10438
  • View blog
  • Posts: 38,657
  • Joined: 27-December 08

Re: Java Code error

Posted 19 May 2010 - 12:19 PM

Can you show us your updated code as well as specifically describe your problems or errors?
Was This Post Helpful? 0
  • +
  • -

#10 Ember  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Re: Java Code error

Posted 19 May 2010 - 12:19 PM

View Postem_123, on 19 May 2010 - 11:08 AM, said:

View Postmacosxnerd101, on 19 May 2010 - 11:05 AM, said:

An easier idea would be to store double variables for totalMiles and totalGallons. Then just divide when you go to calculate the average.


I did try that, but that's my problem, I can't understand how to make a code that would add total number of miles??


Start with a declaration:
int totalMile = 0;



Your probably want to have this either at the beginning of the class definition or the beginning of the method (depending on the scope you want)

Next you update it every time you get an ActionEvent from the Calculate Miles Button.
So plug this into your code accordingly..
totalMiles += miles;


miles being the number retrieved form the miles Text field. The += call is exactly like this, just written differently:
totalMiles = totalMiles + miles;


Then you just do the same for totalGallons and you should be set for an easy division and output.
Was This Post Helpful? 0
  • +
  • -

#11 em_123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-May 10

Re: Java Code error

Posted 19 May 2010 - 12:27 PM


public class Assignment2UI extends javax.swing.JFrame {

    /** Creates new form Assignment2UI */
    public Assignment2UI() {
        initComponents();

 
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();
        jTextField4 = new javax.swing.JTextField();
        jButton5 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Calculate Fuel Efficiency", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 0, 153))); // NOI18N

        jLabel1.setText("Input distance");

        jLabel2.setText("Input fuel consumption");

        jLabel3.setText("Fuel Efficiency");

        jTextField1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField1ActionPerformed(evt);
            }
        });

        jTextField2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField2ActionPerformed(evt);
            }
        });

        jTextField3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField3ActionPerformed(evt);
            }
        });

        jButton1.setText("Calculate Fuel Efficiency");
        jButton1.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jButton1StateChanged(evt);
            }
        });
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton4.setText("Convert to Kilometres per Litre");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 197, Short.MAX_VALUE)
                                .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextField3)
                            .addComponent(jTextField2)
                            .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 66, Short.MAX_VALUE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 163, Short.MAX_VALUE)
                .addComponent(jButton4)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(22, 22, 22)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Calculate Average Fuel Efficiency", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(255, 0, 153))); // NOI18N

        jButton2.setText("Calculate Average Fuel Efficiency");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jTextField4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField4ActionPerformed(evt);
            }
        });

        jButton5.setText("Convert to Kilometres per Litre");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jButton2)
                .addGap(18, 18, 18)
                .addComponent(jTextField4, javax.swing.GroupLayout.DEFAULT_SIZE, 164, Short.MAX_VALUE)
                .addGap(69, 69, 69)
                .addComponent(jButton5)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(73, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton5)))
        );

        jButton3.setText("EXIT");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(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)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jButton3)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(10, 10, 10))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 92, Short.MAX_VALUE)
                .addComponent(jButton3)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>                        

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        System.exit(0);
    }                                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        //Define float variables.
        float num1, num2, result;
        //Parse text to a type float.
        num1 = Float.parseFloat (jTextField1.getText());
        num2 = Float.parseFloat (jTextField2.getText());
        //Write code for calculating Miles Per Gallon.
        result = (float) num1/num2;
        //The following code will send the result to the
        //Fuel Efficiency result box.
        jTextField3.setText(String.valueOf(result)); 

    }                                        

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:        
    }                                           

    private void jTextField3ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        //Define float variables.
        float num1, num2, result, result1;
        //Parse text to a type float.
        num1 = Float.parseFloat (jTextField1.getText());
        num2 = Float.parseFloat (jTextField2.getText());
    
        //Write code for calculating Miles Per Gallon.
        
        result1 = num1/num2;

        result = (float) (282/result1);

        //The following code will send the result to the
        //Fuel Efficiency result box.
        jTextField3.setText(String.valueOf(result));
    }                                        

    private void jTextField2ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:       
    }                                           

    private void jTextField4ActionPerformed(java.awt.event.ActionEvent evt) {                                            
        // TODO add your handling code here:
    }                                           

   
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        
        float num3;
        float total = 0;
        int count = 0;
        float averageMpg = 0;

        num3 = Float.parseFloat(jTextField3.getText());
       


        if (total < 0){

                total+=num3;
                count++;
            }

            averageMpg = total/count;

            jTextField4.setText(String.valueOf(averageMpg));
       
        
    
    }                                        

    private void jButton1StateChanged(javax.swing.event.ChangeEvent evt) {                                      
        // TODO add your handling code here:
    }                                     

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Assignment2UI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    // End of variables declaration                   
}





Edited by macosxnerd101: Code tags are BBCode, not XML, so you need to use code tags like so: :code:

Thanks! :)

~macosxnerd101

This post has been edited by macosxnerd101: 19 May 2010 - 12:30 PM

Was This Post Helpful? 0
  • +
  • -

#12 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10438
  • View blog
  • Posts: 38,657
  • Joined: 27-December 08

Re: Java Code error

Posted 19 May 2010 - 12:38 PM

You should make the variables I described in my last post instance variables. That is declared in the class, but not in any method and without the static modifier. Also, please describe your problems or errors in the future.
Was This Post Helpful? 0
  • +
  • -

#13 em_123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-May 10

Re: Java Code error

Posted 19 May 2010 - 12:40 PM

This bit gives an 'NaN' output in jTextField4.


float num3;
        float total = 0;
        int count = 0;
        float averageMpg = 0;

        num3 = Float.parseFloat(jTextField3.getText());
       


        if (total < 0){

                total+=num3;
                count++;
            }

            averageMpg = total/count;

            jTextField4.setText(String.valueOf(averageMpg));





View Postmacosxnerd101, on 19 May 2010 - 11:38 AM, said:

You should make the variables I described in my last post instance variables. That is declared in the class, but not in any method and without the static modifier. Also, please describe your problems or errors in the future.



what do you mean by this?
Was This Post Helpful? 0
  • +
  • -

#14 em_123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-May 10

Re: Java Code error

Posted 19 May 2010 - 02:22 PM

Can someone please help me with this?? I need to know how to add a number input to a running tally so that I can calculate the TOTAL. This will then be added to another equation in another button to calcuate the AVERAGE.

Input miles: (jTextField1)
Input Gallons: (jTextField2)

Calculate MPG: (jButton1) answer sent to (jTextField3)

I need all results generated in (jTextField3) to go into some sort of tally so that I can calculate the average divided by Number of Entries (generated by a count++ ??)

The other way i could go about it would be to send the Miles input and Gallons input into a tally to get a totalMiles and totalGallons tally.

These will then be calculated using averageMPG = totalMiles/totalGallons.

However, to do this or the other way, I will need to know how get these inputs into some sort of tally so i can get the total. I do not know how to do it! As you can see, I have tried different ways. So if someone could please shed some light as to what I may need to do, that would be awesome! I have managed to get this far on my own, and all i need is some example code to help me out.

Thank you so much

em_123
Was This Post Helpful? 0
  • +
  • -

#15 em_123  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 18-May 10

Re: Java Code error

Posted 19 May 2010 - 02:39 PM

Whereabouts do I put this code? Because i don't have a class as such, they are all private void button commands??



Start with a declaration:
int totalMile = 0;



Your probably want to have this either at the beginning of the class definition or the beginning of the method (depending on the scope you want)

Next you update it every time you get an ActionEvent from the Calculate Miles Button.
So plug this into your code accordingly..
totalMiles += miles;


miles being the number retrieved form the miles Text field. The += call is exactly like this, just written differently:
totalMiles = totalMiles + miles;


Then you just do the same for totalGallons and you should be set for an easy division and output.
[/quote]
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2