Java输入文件循环

时间:2013-11-26 21:05:05

标签: java file loops input

我有一个读取文件

  

Office 1

     

3

     

39 7

     

39 7

     

39 6.5

     

Office 2

     

2

     

19 8

     

19 8

这显示它是哪个办公室,下一个数字是该办公室的经理人数,然后是每个工作小时数,然后是他们每周赚取的数量

我正在尝试创建一个循环,所以我的java文件可以读取小时数然后乘以小时费率然后将其保存为双倍然后移动到下一个办公室并执行相同的操作以便稍后我可以使用双打我的程序,然后循环将在它到达最终办公室时结束我知道如何从文件中读取我正在努力获得一个循环,它可以做我上面指定的

非常感谢任何帮助!

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;

现在这个工作不会让我弄错,但它应该是肯定的长期好吗?

1 个答案:

答案 0 :(得分:1)

很抱歉迟到的回复,我最终为你编写了完整的代码,所以要了解这是如何完成的。

无论如何,如果文件包含数百个办公室条目,那么您目前的工作方式效率不高。

确保您拥有完整文件内容,如帖子中所示。如果某些内容缺失,则以下代码可能会崩溃或为您提供意外结果。

以下是您完成以下要求的代码。在这种情况下,您将无法从文件中保存任何数据,因为您无法使用Arrays。如果你有使用ArrayList的奢侈品,可以使用它来存储文件数据。

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();
        }

    }
}

如果您有任何疑问,请告诉我。祝你好运。