Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Need help with java gui and nested for loop

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with java gui and nested for loop

    Hello all,

    I have created a program(gui) that allows a user to input time and minutes in textfields and then calculates the totals and average wages per hour, to generate a report that displays in a text area. The program works but instead of just calculating the totals, it calculates the totals after each entry, when all I need is just a total of all entries.

    The portion of the code where I believe the problem is below:

    This is my first time using Java and my first time programming, so any help would be greatly appreciated.


    private void runreportButtonActionPerformed(java.awt.event.Acti onEvent evt) {
    // Calculates total minutes of tutoring provided, average wages earned per hour, total earned to date, and determines if average wage per hour is below, average, or above minimum wageTODO add your handling code here:

    int columns = 2;
    int rows = earnings.length;
    double totalTime = 0.00;
    double totalPayment = 0.00;
    double averageWages =0.00;
    double minWage =7.00;
    DecimalFormat df = new DecimalFormat("#.##");

    String report="";
    //add total minutes tutoring and display in text area
    for (int j=0;j<columns;j++){
    //add total earnings and display in text area
    for(int i=0;i<rows;i++){
    report+= earnings[i][j];

    if (j==0){
    totalTime+= earnings[i][j];
    }else if(j==1){
    totalPayment+= earnings[i][j];
    report="\n";
    //Displays in text area
    jTextArea1.append("Total Earnings = $ " +totalPayment+ "\n");
    }
    //Displays in text area
    jTextArea1.append("\n\n");
    jTextArea1.append ("Report of your wages to Date\n\n");
    jTextArea1.append ("\n");
    jTextArea1.append("Total Minutes Spent Tutoring = " +totalTime + "\n");
    //calculates average per hour wage
    if (earnings.length>0){
    averageWages = totalPayment/(totalTime/60);
    //Displays in text area
    jTextArea1.append("Average Per Hour Wage $ " + averageWages + "\n");
    jTextArea1.append("\n\n");
    jTextArea1.append("Minimum Wage is currently $6.55");
    jTextArea1.append("\n\n");

    if(averageWages<minWage){

    jTextArea1.append("Your average wages per hour are below Average");

    }else if(averageWages>=minWage && averageWages<=minWage * 2.00){
    jTextArea1.append("Your average wages per hour are average");

    }else if(averageWages > minWage * 2.00){
    jTextArea1.append("Your average wages per hour are above Average");
    }
    }
    }

    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with java gui and nested for loop

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    Can you explain this:
    totals after each entry, when all I need is just a total of all entries.
    What items are to be added together to get what total?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with java gui and nested for loop

    Quote Originally Posted by Norm View Post
    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    Can you explain this:

    What items are to be added together to get what total?
    **The user enters 3 time amounts and 3 payment amounts for 3 tutor sessions and the report button is supposed to generate a total of all 3 times and a total of all 3 payments and also gives an average wage per hour based on these totals.


    <private void runreportButtonActionPerformed(java.awt.event.Acti onEvent evt) {
    // Calculates total minutes of tutoring provided, average wages earned per hour, total earned to date, and determines if average wage per hour is below, average, or above minimum wageTODO add your handling code here:
     
    int columns = 2;
    int rows = earnings.length;
    double totalTime = 0.00;
    double totalPayment = 0.00;
    double averageWages =0.00;
    double minWage =7.00;
    DecimalFormat df = new DecimalFormat("#.##");
     
    String report="";
    //add total minutes tutoring and display in text area
    for (int j=0;j<columns;j++){
    //add total earnings and display in text area
    for(int i=0;i<rows;i++){
    report+= earnings[i][j];
     
    if (j==0){
    totalTime+= earnings[i][j];
    }else if(j==1){
    totalPayment+= earnings[i][j];
    report="\n";
    //Displays in text area
    jTextArea1.append("Total Earnings = $ " +totalPayment+ "\n");
    }
    //Displays in text area
    jTextArea1.append("\n\n");
    jTextArea1.append ("Report of your wages to Date\n\n");
    jTextArea1.append ("\n");
    jTextArea1.append("Total Minutes Spent Tutoring = " +totalTime + "\n");
    //calculates average per hour wage
    if (earnings.length>0){
    averageWages = totalPayment/(totalTime/60);
    //Displays in text area
    jTextArea1.append("Average Per Hour Wage $ " + averageWages + "\n");
    jTextArea1.append("\n\n");
    jTextArea1.append("Minimum Wage is currently $6.55");
    jTextArea1.append("\n\n");
     
    if(averageWages<minWage){
     
    jTextArea1.append("Your average wages per hour are below Average");
     
    }else if(averageWages>=minWage && averageWages<=minWage * 2.00){
    jTextArea1.append("Your average wages per hour are average");
     
    }else if(averageWages > minWage * 2.00){
    jTextArea1.append("Your average wages per hour are above Average");
     
    }
    >

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with java gui and nested for loop

    supposed to generate a total of all 3 times and a total of all 3 payments
    Does it do that? If not please explain and give an example of what it does do and describe what is wrong with that and say what it should do.

    Your code is not properly formatted. It has no indentations for nesting levels of logic. That makes the code harder to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with java gui and nested for loop

    Quote Originally Posted by Norm View Post
    Does it do that? If not please explain and give an example of what it does do and describe what is wrong with that and say what it should do.

    Your code is not properly formatted. It has no indentations for nesting levels of logic. That makes the code harder to read and understand.
    It does calculate the totals for the times entered and the payments. It also calculated the average per hour wage. The last 5 lines of the output are correct, as follows:

    Report of your Wages to Date

    Total Minutes spent tutoring = 270.0
    Total Earnings = $65.0
    Average wages per hour = $14.44444444445

    Minimum wage is currently $6.55

    Your wages are above average


    however, the previous lines have the exact same thing but with different amounts. I have 5 outputs with the same format, but different totals and averages. It appears that after the user enters a time and payment, the totals calculation is being run, when it only needs to be run once, after the user enters all times and payments.

    I apologize for the incorrect format on the nested loop. I assumed that was my problem, I am just oblivious as to how to correct it.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with java gui and nested for loop

    Your code is not properly formatted. It has no indentations for nesting levels of logic. That makes the code harder to read and understand.
    Can you edit your code and correct its formatting?

    Are you saying that the program computes the totals one time for each of the values entered. If there are 5 values entered, it computes the totals 5 times? But you only want it to do the totaling one time when all 5 inputs have been done?
    How can the program know when all 5 inputs are available? Could you have a button that the user presses when all the input is available so the program would know when it was ready to process?

    So we can see how the code is executing, please add some println statements to the code that print out a message every time the listener method is called and that prints out the results of the computations and the shows the values the user input for those computations.
    Execute the program, copy the printed output from the printlns and paste them here.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with java gui and nested for loop

    tutortask.jpg
    Quote Originally Posted by Norm View Post
    Your code is not properly formatted. It has no indentations for nesting levels of logic. That makes the code harder to read and understand.
    Can you edit your code and correct its formatting?

    Are you saying that the program computes the totals one time for each of the values entered. If there are 5 values entered, it computes the totals 5 times? But you only want it to do the totaling one time when all 5 inputs have been done?
    How can the program know when all 5 inputs are available? Could you have a button that the user presses when all the input is available so the program would know when it was ready to process?

    So we can see how the code is executing, please add some println statements to the code that print out a message every time the listener method is called and that prints out the results of the computations and the shows the values the user input for those computations.
    Execute the program, copy the printed output from the printlns and paste them here.

    Yes, the program computes the totals one time for each of the values entered when all I want is a total of all values. I have a button for the user to press once all values have been entered. It is an"enter" button and when clicked it prints all of this in the text area. I have uploaded an image of the output for your review.

    I am not sure how to do it, but I will try to edit the code and see if I can correct the formatting and then I will post it.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with java gui and nested for loop

    I have a button for the user to press once all values have been entered
    When the button is pressed, the code should sum the detail values and display the results when it is done doing the summing.
    Do the summing inside of the loops.
    Do the display of the totals outside of the loops.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with java gui and nested for loop

    That makes sense. I will work on that. Thank you so much for the help. I will keep you posted on my results.

  10. #10
    Junior Member
    Join Date
    Mar 2012
    Posts
    16
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with java gui and nested for loop

    I went over my code and edited it so that it should have the correct formatting now, however, now nothing will print in the text area when I hit the run report button. Any ideas on what I am doing wrong?
        int columns = 2;
        int rows = earnings.length;
        double totalTime = 0.00;
        double totalPayment = 0.00;
        double averageWages =0.00;
        double minWage =7.00;
     
        String report = new String();
     
                 for (int j=0;j<columns;j++){
                    for(int i=0;i<rows;i++){
                     if (j==0){
                     totalTime+= earnings[i][j];
                     }
                     else if(j==1){
                     totalPayment+= earnings[i][j];
                     }
                 }
             }
                jTextArea1.append("\n\n");
                jTextArea1.append("Report of your wages to Date\n\n");
                jTextArea1.append("\n");
     
             if (earnings.length>0){
                   averageWages = totalPayment/(totalTime/60);
             }
            //Displays in text area  
           jTextArea1.append("Average Per Hour Wage  $   " + averageWages + "\n");
           jTextArea1.append("\n\n");
           jTextArea1.append("Minimum Wage is currently $6.55");
           jTextArea1.append("\n\n");
     
            if(averageWages<minWage){
     
             jTextArea1.append("Your average wages per hour are below Average");
     
         }else if(averageWages>=minWage && averageWages<=minWage * 2.00){
             jTextArea1.append("Your average wages per hour are average");
     
         }else if(averageWages > minWage * 2.00){
            jTextArea1.append("Your average wages per hour are above Average"); 
         }
                jTextArea1.append("Total Minutes Spent Tutoring = " +totalTime + "\n");
                jTextArea1.append("Total Earnings = $  " +totalPayment+ "\n");
            //calculates average per hour wage
     
                jTextArea1.append(report);
    }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with java gui and nested for loop

    Your posted code leaves off the beginning {s so it is hard to make sense of what the code is doing.
    If any of the jTextArea1.append(.... calls are executed, then something should be put into the textarea.
    There are lots of reasons why you can't see it:
    The jTextArea1 component is not visible in the GUI;
    The textarea is cleared after stuff is put into it

    Without seeing all of the code, I don't know why you can not see what was appended to the textarea

    Add some printlns next to where the append method is called to record that the append statement was executed.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Need help with java gui and nested for loop

    Is this the same question?
    http://www.javaprogrammingforums.com...html#post61792
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Nid help in nested loop !!
    By Spice in forum Loops & Control Statements
    Replies: 1
    Last Post: February 14th, 2012, 11:10 AM
  2. Nested loop if statement help
    By CSUTD in forum Loops & Control Statements
    Replies: 7
    Last Post: November 8th, 2011, 02:05 PM
  3. Simple Nested Do/While Loop
    By Farmer in forum Loops & Control Statements
    Replies: 2
    Last Post: July 25th, 2011, 08:31 AM
  4. do while loop with nested while question
    By johneusmc in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 6th, 2010, 04:45 PM
  5. nested loop
    By b109 in forum Java Theory & Questions
    Replies: 1
    Last Post: May 30th, 2010, 10:05 AM