Basam
Basam

Reputation: 41

How do I update the variables changed in a for loop?

I'm an AP computer science student and here is the assignment
Program Description: You just started working as a programmer. You've agreed to the following compensation package.

• You are paid $30 an hour. • You earn an extra $25.50 an hour for any part of a day where you work more than 8 hours.
• You earn an extra $15 an hour for hours beyond 40 in any one week.
• You also earn a 125% bonus on any amount you earn for working on Saturday, and a 50% bonus for working Sunday.

Work hours that I used , stored as txt for file reader

9 8 10 8 9 9 5   
7 8 8 8 0 8 9   
6 10 5 0 0 0 0   
8 8 8 8 8 8 8   
6 6 6 6 6 6 6  
10 3 4 5 7 5 4  
8 8 8 8 8 0 0  

Sample Output: (be sure to run several test cases of your own by adding additional data to your data file)

Hours Worked: 9 8 10 8 9 9 5        
Week # 1 $2581.88   
Hours Worked: 7 8 8 8 0 8 9    
Week # 2 $2033.25   
Hours Worked: 6 10 5 0 0 0 0  
Week # 3 $681   
Hours Worked: End of reading from file.   

Vs My output  
Total Hours Worked: 58

Week #1 $2010.00

Total Hours Worked: 48

Week #2 $1560.00

Total Hours Worked: 21

Week #3 $630.00

Total Hours Worked: 56

Week #4 $1920.00

Total Hours Worked: 42

Week #5 $1290.00

Total Hours Worked: 38

Week #6 $1140.00

Total Hours Worked: 40

Week #7 $1200.00

Hours Worked: End of reading from file. 

The problem That I see is that what ever happens inside the for loops doesn't affect the outside of my code and I find that confusing.

Please don't change the entire code, I also want to understand the code at my current level so I can learn and progress, Thank you!

import java.util.*;
import java.io.*;
import java.text.*;
public class wages {
    public static void main(String[] args) {
        Scanner inFile = null;
        int TotalW = 0; // total hours per week
        int TotalD = 0; // total hours per day
        double Wage = 0.0; // Wage for the weekdays
        double WageS = 0.0; // wage for sat
        double WageSu = 0.0; // wage for Sundays
        double Total = Wage + WageS + WageSu;
        int week = 1; // week # counter
        DecimalFormat TD = new DecimalFormat("0.00"); // formatter
        try {
            // Create a scanner to read the file, file name is parameter
            inFile = new Scanner(new File("Prog213a.txt"));
        } catch (FileNotFoundException e) {
            System.out.println("File not found!");
            // Stop program if no file found
            System.exit(0);
        }

        while (inFile.hasNext()) // as long as there is a next variable...
        {
            for (int x = 1; x <= 5; x++) // count the week days / first 5 variables
            {
                TotalD = inFile.nextInt();
                if (TotalD > 8) Wage = Wage + ((TotalD - 8) * 25.50); // bonus if worked above 8 hours per day

                TotalW = TotalW + TotalD;
            }
            for (int x = 1; x <= 1; x++) {
                TotalD = inFile.nextInt();
                if (TotalD > 8) WageS = WageS + ((TotalD - 8) * 25.50);
                WageS = WageS + (WageS * 2.25);
                TotalW = TotalW + TotalD;
            }

            for (int x = 1; x <= 1; x++) {
                TotalD = inFile.nextInt();
                if (TotalD > 8) WageSu = WageSu + ((TotalD - 8) * 25.50);
                WageSu = WageSu + (WageSu * 2);
                TotalW = TotalW + TotalD;

            }

            if (TotalW > 40) Total = Total + ((TotalW - 40) * 15);


            Total = Total + (TotalW * 30);
            System.out.println("Total Hours Worked: " + TotalW);
            System.out.println("Week #" + (week++) + " $" + TD.format(Total));

            TotalW = 0;
            TotalD = 0;
            Wage = 0;
            WageS = 0;
            WageSu = 0;
            Total = Wage + WageS + WageSu;
        }
        System.out.println("Hours Worked: End of reading from file. ");
    }
}

Upvotes: 2

Views: 198

Answers (2)

Kyle Gowen
Kyle Gowen

Reputation: 467

If I am understanding your problem, you may have a scope issue.

Any variables declared inside of a scope (anything inside curly braces) only lasts for as long as you are in that scope.

In your case, your scope is the for loop.

For instance.

(for int x = 0; x < 10; x++){
    int y = y+x;
}

Your y value here would be inaccessible outside of the for loop.

However,

if you did:

int y = 0;
(for int x = 0; x < 10; x++){
    y = y+x;
}

Your y value would be saved and usable elsewhere since it was declared outside of the scope of the for loop.

I hope this answers your question.

I also see that your for loop conditions are x<=1 where you are declaring x=1. This will cause your loop to only run once. Maybe that's what you intended. I am not sure.

Good luck.

Upvotes: 3

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16152

First, use your IDEs autoformat to properly format your code; easy to read code is easy to reason about.

Secondly, your for loops are like this:

              for(int x=1; x<=1; x++) {...}

Think about how many times the body of the loop will get executed.

Upvotes: 0

Related Questions