Java输入文件循环 [英] Java input file loop

查看:123
本文介绍了Java输入文件循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个读取

办公室1

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

我正在尝试创建一个循环,以便我的java文件可以读取小时数,然后将它们乘以小时率,然后将其保存为双精度型,然后移至下一个办公室并执行相同的操作,因此以后可以在其中使用双精度型我的程序,那么当到达最后一个办公室时,循环将结束,我知道如何从文件中读取我只是在努力获取一个循环,该循环可以完成我在上面指定的操作

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

非常感谢任何帮助!

编辑......

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?

推荐答案

抱歉,我的回复很晚,我最终为您编写了完整的代码,因此请了解如何完成此工作.

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

如果文件包含数百个Office条目,无论如何,当前的操作方式都是无效的.

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.

这是您满足以下要求的代码.在这种情况下,您将无法从文件中保存任何数据,因为您无法使用Arrays.可能是如果您愿意使用ArrayList,然后利用它来存储文件数据.

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.

这篇关于Java输入文件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆