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");
}
}
}
}
}
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:
Quote:
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?
Re: Need help with java gui and nested for loop
Quote:
Originally Posted by
Norm
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.
Code java:
<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");
}
>
Re: Need help with java gui and nested for loop
Quote:
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.
Re: Need help with java gui and nested for loop
Quote:
Originally Posted by
Norm
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.:confused:
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.
1 Attachment(s)
Re: Need help with java gui and nested for loop
Attachment 1126
Quote:
Originally Posted by
Norm
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.
Re: Need help with java gui and nested for loop
Quote:
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.
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.
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?
Code java:
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);
}
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.
Re: Need help with java gui and nested for loop