Results 1 to 5 of 5
  1. #1
    scottdaking is offline Member
    Join Date
    Feb 2013
    Location
    Baltimore, MD
    Posts
    10
    Rep Power
    0

    Default Need help calculating totals from values in a 2D array

    OK...I have one LAST obstacle in this program, designed to record and report tutoring sessions. My intention is to total both variables (totalWages and totalMinutes) being input and stored in a 2D array. I must use a for loop in the assignment, so I imagined that this would be the best application of that loop structure.

    When I run the Run Report Button method (to get the totals), the variables only output the last input values, not the total of all input values. Once again, I appreciate any assistance that is offered.

    -Scott


    Java Code:
    private void enterButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
            // TODO add your handling code here:
            
            double time = Double.parseDouble(sessionLength.getText());
            double earnings = Double.parseDouble(earningsReceived.getText());
            int i = 0;
            
            //each time user enters the time spent and earnings received for each
            //tutor session, those values will be added to the array.
            
            //positive input warning
            if (time <= 0 || time > 240 || earnings <= 0) {
                Component frame = null;
                JOptionPane.showMessageDialog(frame,"Invalid input.  Please try again.", "Invalid Input Warning", JOptionPane.WARNING_MESSAGE);
            }
            else {
                tutorArray[i][0] = time;
                tutorArray[i][1] = earnings;
            
             //display time and earnings so far
             dataTextArea.append("Minutes =" + " " + time + "      " + "Earnings = $" + earnings + "\n");
             i++;
            }
        }                                           
    
        private void runReportButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
            // TODO add your handling code here:
                  
            //calculate the total wages earned 
            for (int s = 0; s < tutorArray.length - 1; s++) {
                for (int t = 0; t < tutorArray[0].length; t += 2){
                    totalMinutes = totalMinutes + tutorArray[s][t];
                    totalWages = totalWages + tutorArray[s][t + 1];
                    }
                }
            //convert total minutes of tutoring to hours
            totalHours = totalMinutes / 60;
            
            //then calculate average wage per hour
            averageWage = totalWages / totalHours;
            
            //print report
            dataTextArea.append("\n\n**************************");
            String line;
            line = String.format("\n\nReport of your wages to Date:" + "   " + "%7.2f", totalWages);
            dataTextArea.append(line);
            line = String.format("\n\nTotal Minutes Spent Tutoring =" + "   " + "%7.2f", totalMinutes);
            dataTextArea.append(line);
            line = String.format("\nTotal Earnings =" + "   " + "%7.2f", totalWages);
            dataTextArea.append(line);
            line = String.format("\nAverage Per Hour Wage = $" + "   " + "%7.2f", averageWage);
            dataTextArea.append(line);
                   
            //determine whether average wages per hour are average, above or below average, then print
            
            line = String.format("\n\nMinimum Wage is currently = $" + MINIMUM_WAGE);     
            dataTextArea.append(line);
            
            if (averageWage < MINIMUM_WAGE) {
                line = String.format("\n\nYour average wages per hour are below average.");
                dataTextArea.append(line);
                }
            
                else if (averageWage > MINIMUM_WAGE) {
                    line = String.format("\n\nYour average wages per hour are above average.");
                    dataTextArea.append(line);
                    }
                
                    else {
                        line = String.format("\n\nYour average wags per hour are average.");
                        dataTextArea.append(line);
                        }
        }                                               
    
        private void quitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
            // TODO add your handling code here:
            System.exit(0);
        }

  2. #2
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: Need help calculating totals from values in a 2D array

    Hi Scott,

    Without looking at the complete code I can only take an educated guess at your implementation.

    Your code in the method enterButtonActionPerformed() declares 'i' as 0. Lines 17 and 18 then assign time and earnings to column 'i' each time the method is called. As 'i' is redeclared as 0 each time, the previous entry for column 0 is being overwriting the previous entry.
    The code in runReportButtonActionPerformed() is looping through the arrays correctly but because previous entries in the array are being overwritten, only the last entry exists in the array.

    Regards.

  3. #3
    scottdaking is offline Member
    Join Date
    Feb 2013
    Location
    Baltimore, MD
    Posts
    10
    Rep Power
    0

    Default Re: Need help calculating totals from values in a 2D array

    Thanks for your response, Ronin, my friend! Just as you determined, I ended up moving that variable initialization up as a global, so it wouldn't overwrite the array values. Now, it's calculating correctly, but I had to move the following line:

    Java Code:
    dataTextArea.append("Minutes =" + " " + time + "      " + "Earnings = $" + earnings + "\n");
    inside the "for" loop in the RunReportButton method (per my instructions) and the printout only displays the last values input on each line. I LOVE programming...

  4. #4
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: Need help calculating totals from values in a 2D array

    As with 'i' was previously 'time' and 'earnings' are being redefined each time you call enterButtonActionPerformed().
    Only 'i' needs to be declared a global variable, 'time' and 'earnings' can be left within the method as these values have already been passed into the array.

    You were right to move the following code into the runReportButtonActionPerformed() method, this is a more logical place to put it.
    Java Code:
    dataTextArea.append("Minutes =" + " " + time + "      " + "Earnings = $" + earnings + "\n");
    Instead of using 'time' and 'earnings' you could call use the values held in the array.
    Java Code:
    dataTextArea.append("Minutes =" + " " + tutorArray[s][t] + "      " + "Earnings = $" + tutorArray[s][t+1] + "\n");
    Regards.

  5. #5
    scottdaking is offline Member
    Join Date
    Feb 2013
    Location
    Baltimore, MD
    Posts
    10
    Rep Power
    0

    Default Re: Need help calculating totals from values in a 2D array

    You are the MAN!!!!!! Thank you, thank you, THANK YOU!!!! I was about to ruin a perfectly good 22" monitor...

    Enjoy your day, Ronin!

    -Scott

Similar Threads

  1. Calculating values from a jtable column
    By victaino in forum New To Java
    Replies: 37
    Last Post: 05-24-2012, 11:16 AM
  2. Blackjack Program Not Calculating Totals Correctly
    By skaterboy987 in forum New To Java
    Replies: 1
    Last Post: 10-19-2011, 08:59 PM
  3. Array help! - Grand totals
    By Gaebril in forum New To Java
    Replies: 23
    Last Post: 04-18-2011, 12:02 AM
  4. calculating with values out of another class
    By senca in forum New To Java
    Replies: 3
    Last Post: 04-08-2010, 03:01 PM
  5. Calculating min and max in an array list
    By Jamison5213 in forum New To Java
    Replies: 2
    Last Post: 01-17-2010, 10:09 PM

Posting Permissions

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