Java input file loop

2019-09-01 03:13发布

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?

1条回答
Summer. ? 凉城
2楼-- · 2019-09-01 03:48

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.

查看更多
登录 后发表回答