1

我有一个数据集,它在字符串数组中列出每天完成的不同活动,类似的概念在这里问。每个活动都是分隔的,可以很容易地分成列,因为我在 Excel 中没有问题。

activites
Work | family | date | gaming | relax | good sleep | shopping   
Work | family | date | Nature | Crusin | reading | gaming | relax | good sleep | cooking | laundry  
family | date | movies & tv | gaming | sport | relax | medium sleep | cooking   
Work | family | date | Photography | gaming | relax | good sleep | medium sleep | cooking   
Work | family | date | Nature | reading | gaming | relax | good sleep | cleaning    

我想要做的是将每个活动变成一个布尔变量,它有自己的列,因此它表示0当天没有完成活动并且1已经完成了活动。它看起来像这样:

Work   Family   Date   Gaming   Relax
1      1        0      1        0
1      1        1      0        0
0      0        1      0        1
4

1 回答 1

0

所以,我最终做的是利用我对 Java 的了解来重新格式化数据。我首先将活动分成各自的变量,每个变量都包含一个数字(二进制​​)值,以指示该活动当天是否完成。我不得不单独对待睡眠质量,所以这部分看起来有点不稳定。这是产生正确输出的代码:

    public static void main(String[] args) throws FileNotFoundException {
        Scanner scan = new Scanner(new FileReader("activities.txt"));
        String[] actList = { "Work", "school", "family", "friends", "date", "nature", "crusin", "photography",
                "making music/piano", "movies & tv", "reading", "gaming", "sport", "relax", "sleep", "shopping", "cleaning",
                "cooking", "laundry" };
        int row = 0;
        while (scan.hasNextLine()) {
            row++;
            int col = 0;
            int activityNo = 0;
            int[] actValue = new int[actList.length];
            String pipeDelim = scan.nextLine();
            String[] actName = pipeDelim.split(" \\| ");
            int sleepTagsUsed = 0;
            while (activityNo < actName.length) {
                col = 0;
                for (String a : actName) {
                    if (a.contains("sleep")) {
                        col = 14;
                        if (a.equalsIgnoreCase("bad sleep") || a.equalsIgnoreCase("bad sleep\t")) {
                            if (col < actList.length) {
                                actValue[col] = 0;
                                if (sleepTagsUsed == 0) {
                                    col++;
                                }
                                sleepTagsUsed++;
                            } else {
                                break;
                            }
                            if (!(activityNo > actName.length)) {
                                activityNo++;
                            }
                        } else if (a.equalsIgnoreCase("medium sleep") || a.equalsIgnoreCase("medium sleep\t")) {
                            if (col < actList.length) {
                                actValue[col] = 1;
                                if (sleepTagsUsed == 0) {
                                    col++;
                                }
                                sleepTagsUsed++;
                            } else {
                                break;
                            }
                            if (!(activityNo > actName.length)) {
                                activityNo++;
                            }
                        } else if (a.equalsIgnoreCase("good sleep") || a.equalsIgnoreCase("good sleep\t")) {
                            if (col < actList.length) {
                                actValue[col] = 2;
                                if (sleepTagsUsed == 0) {
                                    col++;
                                }
                                sleepTagsUsed++;
                            } else {
                                break;
                            }
                            if (!(activityNo > actName.length)) {
                                activityNo++;
                            }
                        } else if (a.equalsIgnoreCase("sleep early") || a.equalsIgnoreCase("sleep early\t")) {
                            if (col < actList.length) {
                                actValue[col] = 3;
                                if (sleepTagsUsed == 0) {
                                    col++;
                                }
                                sleepTagsUsed++;
                            } else {
                                break;
                            }
                            if (!(activityNo > actName.length)) {
                                activityNo++;
                            }
                        } else {
                            if (col < actList.length) {
                                actValue[col] = -1;
                            } else {
                                break;
                            }
                            System.out.println("No sleep logged error");
                        }
                    } else {
                        int j = 0;
                        for (String i : actList) {
                            if (a.equalsIgnoreCase(i) || a.equalsIgnoreCase(i + "\t")) {
                                    actValue[col] = 1;
                                if (activityNo > actName.length) {
                                    break;
                                } else {
                                    activityNo++;
                                    break;
                                }
                            } else {
                                if (col < actList.length) {
                                    j++;
                                    if (j > col) {
                                        actValue[col] = 0;
                                        col++;
                                    }
                                } else {
                                    break;
                                }
                            }
                        }
                        col++;
                    }
                }

            }
            for (int p : actValue) {
                System.out.print(p + "\t");
            }
            System.out.println();
        }
        scan.close();
    }
于 2020-07-02T04:10:12.167 回答