Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners nested for loops

Have Programming Question? Ask it here!
 
 
 


Zubeda
nested for loops
2 Answer(s)      2 years and 5 months ago
Posted in : Java Beginners


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.

This is my first time using java and my first time programming, but I am guessing that the problem lies with my nested for loop which calculates all information after every user input instead of waiting until all inputs have been entered and then calculating the totals.

the code is as follows. If anyone has any ideas or advise, I would really appreciate it.

Thank you.

private void
  runreportButtonActionPerformed(java.awt.event.ActionEvent
  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;
final int rows = earnings.length;
double totalTime = 0.00; 
double totalPayment = 0.00;
double averageWages =0.00;
double minWage =7.00;

String report="";

//adds total tutoring and displays 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++){

 if (j==0){
     totalTime+= earnings[i][j];

 }else if(j==1){
        totalPayment+= earnings[i][j];

     //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"); 
              jTextArea1.append("Total Earnings = $  "  + totalPayment+
  "\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");
           report+="\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"); 
       }
             }
       }
          }

Advertisement
View Answers

March 23, 2012 at 11:18 AM


Close your loops before going to append the data to textarea.

         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];
                 }
             }
         }


March 23, 2012 at 11:08 PM


Thank you for the response. I did do this, however, now when I click the run report button, nothing displays to the text area.

This is what the code looks like now. Any ideas of 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);
} 


Related Tutorials/Questions & Answers:
nested loops
nested loops  please tell input to get the output: 12345 23456 34567 45678
nested for loops
nested for loops  Hello all, I have created a program(gui... that the problem lies with my nested for loop which calculates all information..."); } } } }   Close your loops before going
Advertisements
Using Nested loops
Using Nested loops  How to use nested loops in java when I want to print the 10 multiples of numbers 2 to 15(in multiplication table)   public class MultiplicationTable{ public static void main(String[] args) { int
I need to output number diamond using nested for loops.
I need to output number diamond using nested for loops.  How to i output this using nested for loops 1 131 13531 1357531 135797531 1357531 13531 131 1
Loops
Loops  Write code that uses nested loops to print the following patterns: Pattern1: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 Pattern 2: 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Loops
Loops  Write code that uses nested loops to print the following patterns: Pattern1: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 Pattern 2: 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1
Write a program that (algorithmically) shows a times table.(convert integers to strings, format text, nested loops)
Write a program that (algorithmically) shows a times table.(convert integers to strings, format text, nested loops)   Write a java program..., nested loops) 0 1 2 3 4 5 6 7 8 9 0 0 0 0 0 0 0
Loops
Loops  by using drjava q1.Write code that uses nested loops to print the following patterns: Pattern1: 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 1 2 3 4 5 6 Pattern 2: 1 2 3 4 5 6 1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 q2.Write code
Loops
Loops  ??Using for loops, Write a program to simulate a clock countdown. The program prompts the user to enter the number of seconds, displays a message at every second, and terminates when the time expires. Use method
Nested class
Nested class  What is nested class?  when a class is defined within another class then such a class is called a nested class. Nested... nested classes are: It is a way of logically grouping classes that are only used
Loops
3.10. Loops Loops are the essential part of the program that have.... More than one loops can be used several times in a script. Loops makes easy... instructed for repetition of the code. PHP Loops In PHP, like other programming
Nested try
Nested try  Hi, In eclipse without providing input i am getting different outputs ie my exception gets executed first then finally block and vice versa.pl explain me class Demo { static void nestedTry(String args[]) { try
nested for-loop
nested for-loop  hi.write a program that will prompt the user for a positive integer(num) and then display two triangles with num number of lines to represent the following patterns of asterisks. the input num must be validated
Nested try
Nested try  Each time when i execute this code in eclipse i get different answers pl help me with this code class Demo { static void nestedTry..., visit the following link: Nested Try Example
pattern-using loops
pattern-using loops  Write a program that displays the following pattern ... (use nested loops) * ** * ** * ** *   the correct pattren
program to create nested tables
program to create nested tables  program to create nested tables
Nested classes: Examples and tutorials
Nested classes: Examples and tutorials       Nested classes Here is another advantage of the Java... within another class, such class is called a nested class. Inner classes can
The nested class - Java Beginners
The nested class  Hi, What is nested class in Java? What is the benefits of nested class? Write examples of nested class. Thanks   .../java/n/java-nested-class.shtml Thanks
use of loops and screenshots
use of loops and screenshots  1.how to use loops in netbeans? 2.how to draw a correct screenshot
Java - Declaring variables in for loops
Java - Declaring variables in for loops  Java - Declaring variables in for loops
GROUP BY IN NESTED QUERY
GROUP BY IN NESTED QUERY  Hello Every One, Query to use GROUP BY CLAUSE IN NESTED QUERY SELECT STATEMENT in mysql database.If u know please help me.thanks
Java nested class example
Java nested class example  Give me any example of Nested Class.   Nested Class: Class defined within another class is called nested class. Nested class is divided into two parts- 1.Static 2.Non-static Static
Use javascript loops..
Use javascript loops..  Write a Javascript code to create a redirection script based on day of the week
How to read loops in Java
How to read loops in Java  Example that explains how to read loop in Java
Need help with nested queries
Need help with nested queries  Hello, Table1 has "id", "votes" columns. Table2 has "id", "Name" column. I need to retrieve Name and its corresponging Votes. I tried with nested queries but its confusing. Can anyone please help
Nested If Statement
Nested If Statement       In this section you will study about the Nested-if Statement in jsp. Nested If statement means to use the if statement inside the other if statement
Nested and Inner classes
Nested and Inner classes  What is the significance of Inner Classes and Static Inner Classes? or Why are nested classes used?   Hi Friend... a nested class. Inner class is a non static class declared inside another class.Non
LOOPS - Java Interview Questions
LOOPS   how to find out unique number of elements in given array?{1,2,5,3,2,1,1,7,2,3,0,1,5} output is: 1-4 2-3 3-2 5-2 0-1 7-1 i want source code plz help me
loops , control structures
loops , control structures  a simple java program to print 1 1 2 1 1 2 3 2 1 1 2 3 4 3 2 1
Nested Loop - Java Beginners
Nested Loop  Hi, Can anyone tell me example of Nested loop in Java? Thanks   Hi friend,public class NestedExample{ public static void main(String[] args){ /** Print Pattern I */ System.out.println("
Nested Ajax--not getting output
Nested Ajax--not getting output  Hi everyone... I have two.jsp page from that , with the help of ajax i have called three.jsp. So, i have used DIV in my jsp pages . on button click,It gives me my desired answer. Same,in next
Static Nested Classes
Static Nested Classes       A nested class that is declared static is called a static nested class. Memory to the objects of  any static nested classes are allocated
Inner Nested Classes
Inner Nested Classes       Non-static nested classes are slightly different from static nested classes, a non-static nested class is actually associated
while loop to for loops
while loop to for loops  I have to change this while loops to for loops and I was wondering if you could help me out. I got the codes just need to know how to change it. int currentNum= num1; while(num1>num2
Use javascript loops..
Use javascript loops..  Write a JavaScript code to find a number of unique letters in string. (Eg. if keyword is Tajmahal, Tajmahal count will be '5' , it only takes these letters T,j,m,h,l , not taken the letter a because
how to use group by in nested query
how to use group by in nested query  Hai, How to use group by clause in nested query select statement in subquery i am having where clause.please help me.thanks in advance
Use javascript loops..
Use javascript loops..  Write a Javascript code, so that numbers...(" "); for(var k=q; k>=1;k--){ document.write(" "); } for(var j=1;j<=i;j++){ document.write(" "); document.write(j); } document.write("<br>"); q
LOOPS !! - Java Interview Questions
LOOPS !!  i have string "Hello" i want to print like H He Hel Hell Hello o ol oll olle olleh i want source code plz help me?  String s="Hello"; System.out.println(s.length()); System.out.println
nested select tag
nested select tag  My requirement is as follows suppose combobox to select country whenever i select a country it will show corresponding states in another combo box.   JSP Code 1)country.jsp: <%@page import
Arrays, loops and string handling
Arrays, loops and string handling  How would i go about in creating a program to mark and grade a set of multiple choice test results. this is a console program that uses JOptionPane dialog boxes as well. Read a set of 10
Nested classes
Nested classes      ... are known as nested classes. Inner classes can be either named... class looks just like any other class, except that it is nested inside another
read string - using loops in Java
read string - using loops in Java  Write a program to read a string composed of an unknown number of words, then count the number of words in the string, and Display the longest and shortest words, with first letter Uppercase
How to display nested ArrayList in JSF
How to display nested ArrayList in JSF  I have a class... to display it in jsf but i dont know how and i am getting mad. I have nested... but i dont know how and i am getting mad. I have nested "foreach" tags
Java inner class and static nested class
Java inner class and static nested class  Java inner class and static nested class
Hibernate Criteria Nested Properties
Hibernate Criteria Nested Properties Consider the example based on Hibernate Criteria Nested Properties DetachedCriteria detachedCriteria...); List list = criteria.list(); An example of Hibernate Criteria Nested
 

 

 

DMCA.com