首页 > 解决方案 > Java解析和计算文本文件

问题描述

我有一个项目,我从很多方面接触过,但我似乎无法做到正确。它是一个 android 应用程序的一部分,它只记录打卡时间并保存到外部存储中的 txt 文件中。txt 文件的格式如下:

PUNCHIN 周三 05-15 03:50 PM

周三 05-15 03:50 PM

PUNCHIN 周三 05-15 03:50 PM

周三 05-15 03:50 PM

PUNCHIN 周三 05-15 03:50 PM

周三 05-15 03:50 PM

PUNCHIN 周三 05-15 03:50 PM

周三 05-15 03:50 PM

它将记录周日至周六的进出次数。

当应用程序读取文件时,它会创建一个数组列表,它是这些行中的每一行。

我有另一个数组列表,它对应于定义为浮点数的每个数组元素,该浮点数表示该打卡当天的小时数。例如:元素一将是 15.83,依此类推。

然后它会调用一个方法从奇数元素中减去偶数元素,并给出当天总共工作了多少小时。

在这一点上,我意识到我的方法有点不对劲。我只想获取从文本文件构建的数组列表,然后计算每天工作了多少小时。

每个人对此的最佳方法的想法是什么?

在这种方法中,我有许多数组列表和子字符串字符串,无穷无尽的 for 循环和大量意大利面条代码。

任何人的前景将不胜感激。谢谢,乔希!

标签: java

解决方案


这是我来解决此任务的功能:

public void calculate (View v) {

    try {
        String view_data = ((TextView) findViewById(R.id.history_view)).getText().toString();

        String[] data_array;
        data_array = view_data.split("\n");

        ArrayList<String> time_punches = new ArrayList<String>();
        // create arrayList of full data line
        for (int a = 0; a <= data_array.length - 1; a++) {
            if (!data_array[a].equals("")) {
                if (!data_array[a].equals("UPS")) {
                    if (!data_array[a].equals("Turkey Hill")) {
                        time_punches.add(data_array[a]);
                    }
                }
            }
        }

        // create another arrayList of just the times, in string format
        ArrayList<String> nextList = new ArrayList<String>();
        for (int er = 0; er <= time_punches.size() - 1; er++) {

            if (time_punches.get(er).substring(0, 7).equals("PUNCHIN")) {
                nextList.add(time_punches.get(er).substring(18, 23));
            }

            if (time_punches.get(er).substring(0, 8).equals("PUNCHOUT")) {
                nextList.add(time_punches.get(er).substring(19, 24));
            }
        }

        // create another arrayList, converting string time format to floats
        int num_a;
        float num_b;
        float num_c;
        float num_d;
        ArrayList<Float> anotherList = new ArrayList<Float>();
        float[] floatArray = new float[nextList.size()];

        for (int _x = 0; _x <= nextList.size() - 1; _x++) {
            num_a = Integer.parseInt(nextList.get(_x).substring(0, 2));
            num_b = Integer.parseInt(nextList.get(_x).substring(3, 5));
            num_a = (num_a) + 12;
            num_b = (num_b) / 60;
            num_c = (num_a) + (num_b);
            anotherList.add(num_c);
            num_a = 0;
            num_b = 0;
            num_c = 0;
            num_d = 0;
        }

        // create yet again, another arrayList to calcuate the time punches grouped by two
        ArrayList<Float> calcList = new ArrayList<Float>();
        float num_e;
        int num_f;
        for (int _z = 0; _z <= anotherList.size() - 1; _z++) {
            num_f = (_z) + 1;
            if ((_z % 2) == 0) {
                calcList.add(anotherList.get(num_f) - anotherList.get(_z));
            }
        }

        // AT THIS POINT calcList contains the hours worked per day.

        // Now do something useful with this data (Hours worked per day)
        float all_hours = 0;
        for (int _y = 0; _y <= calcList.size() - 1; _y++) {
            all_hours += calcList.get(_y);
        }

        ArrayList<String> pretty_list = new ArrayList<String>();
        for (int _a = 0; _a <= time_punches.size() - 1; _a++) {
            if ((_a % 2) == 0) {
                pretty_list.add(time_punches.get(_a).substring(8, 18));
            }
        }

        ArrayList<String> pretty2 = new ArrayList<String>();
        for (int _s = 0; _s <= pretty_list.size() - 1; _s++) {
            pretty2.add(pretty_list.get(_s) + ": " + Float.toString(calcList.get(_s)) + "hours");
        }
        pretty2.add("\nTotal Hours Worked: " + Float.toString(all_hours) + "hours");

        String final_output_string = "";

        for (int _e = 0; _e <= pretty2.size() - 1; _e++) {
            final_output_string = final_output_string + pretty2.get(_e) + "\n";
        }

        // final_output_string is the end result, time_punches contains the raw data, calclist contains the floats of the raw data,
        // allhours is the float sum of all hours worked. Next, just set text to my textview and hope the user doent click the button twice! :)
        // oh, yeah. and all of this is assuming that all hours worked are in the PM. heh... version 2 will be better, hehe

        ((TextView) findViewById(R.id.history_view)).setText(final_output_string);

        alert("Punch Log Converted To Calculated Hours");

    } catch (Exception e) {
        alert("You May Still Be Punched In! Reload Time Punches!");
    }

}

推荐阅读