0

I have a file which reads

Office 1

3

39 7

39 7

39 6.5

Office 2

2

19 8

19 8

This shows which office it is, the next number down is the number of managers in that office then the number of hours each works followed by what they earn per week

I am trying to create a loop so my java file can read the hours then multiply them by the hourly rate then save it as a double then move onto the next office and do the same so I can use the doubles later on in my program, then the loop will end when it gets to the final office I know how to read from the file I am just struggling to get a loop which can do what I specified above

Any help would be much appreciated thanks so much!

EDIT......

public static void main(String[] args) throws FileNotFoundException 
{
Scanner inFile = new Scanner(new FileReader("ExternalData.txt"));
String name;
double employees;
double OfficeRate; 
double OfficeHours; 
double OfficeRate2; 
double OfficeHours2; 
double OfficeRate3; 
double OfficeHours3; 
double OfficeRate4; 
double OfficeHours4; 
name = inFile.nextLine (); 
employees = inFile.nextDouble(); 
OfficeRate = inFile.nextDouble(); 
OfficeHours = inFile.nextDouble();
OfficeRate2 = inFile.nextDouble(); 
OfficeHours2 = inFile.nextDouble();
OfficeRate3 = inFile.nextDouble(); 
OfficeHours3 = inFile.nextDouble();
OfficeRate4 = inFile.nextDouble(); 
OfficeHours4 = inFile.nextDouble();
double Employee1 = OfficeRate * OfficeHours;
double Employee2 = OfficeRate2 * OfficeHours2;
double Employee3 = OfficeRate3 * OfficeHours3;
double Employee4 = OfficeRate4 * OfficeHours4;

double weeklypay = Employee1 + Employee2 + Employee3 + Employee4;

Now this works dont get me wrong but it's well to long than it should be surely?

13
  • You say you are struggling to create "a" loop. Perhaps you need to think about more than one loop. Commented Nov 26, 2013 at 21:11
  • i have just been trying a few and nothing has worked, I'm not to sure which type of loop would work best Commented Nov 26, 2013 at 21:14
  • you can probably accomplish this in a relatively error-free way, however you may be much better off long term learning how to create structured data files, such as XML for this case. XML would allow you to know exactly where you are in the document's records at any given time, instead of having to assume you are at a certain spot based on number of lines down from the top of the file, etc. Commented Nov 26, 2013 at 21:16
  • 1
    i'll edit my post with the code i tried in now Commented Nov 26, 2013 at 21:41
  • 1
    I am on a placement and this is a task we have to do, we can't use arrays so i was wondering if anyone else had any ideas Commented Nov 26, 2013 at 22:40

1 Answer 1

1

Sorry for late reply and I have ended up writing whole code for you, so understand how this is accomplished.

Anyhow the way you are currently doing is not efficient in case file contains hundreds of office entries.

Just make sure you have intact file content as you have shown in your Post. If something is missing then this following code could get crash or give you unexpected results.

Here is code for you to accomplish your following requirements. In this case you won't be able to save any data from file as you can't use Arrays. May be if you have luxury to use ArrayList then make use of that to store file data.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class ScannerReadFile {

    public static void main(String[] args) {

        // Location of file to read
        File file = new File("Your File Path");

        try {

            Scanner scanner = new Scanner(file);
            scanner.useDelimiter("\\s+");
            String line;
            String office = null;

            while (scanner.hasNext()) {
                line = scanner.next();
                if (line.equalsIgnoreCase("Office")) {
                    office = line + scanner.next();
                    // System.out.println(office);
                    continue;
                } else {
                    int empCount = Integer.parseInt(line);

                    double weeklyPay = 0.0;
                    for (int i = 0; i < empCount; i++) {
                        double empPay = Double.parseDouble(scanner.next())
                                * Double.parseDouble(scanner.next());
                        System.out.println("Employee pay: " + empPay);
                        weeklyPay += empPay;
                    }
                    System.out.println("All Employee pay: " + weeklyPay
                            + " for " + office);
                }
            }
            scanner.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }
}

If you have any questions let me know. Good Luck.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.