将数组中的分隔字符串值分隔为布尔变量

Separate delimited string values in array into boolean variables

我有一个数据集,它在一个字符串数组中列出了每天完成的不同活动,类似的概念被问到 。每个 activity 都是分隔的,可以很容易地分成几列,正如我在 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    

我想做的是将每个 activity 变成一个布尔变量,它有自己的列,所以它表示 0 没有完成 activity那天和 1 完成了 activity。它看起来像这样:

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

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

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