Separate delimited string values in array into boolean variables

142 Views Asked by At

I have a dataset that lists different activities done in each day in a string array, similar concept asked here. Each activity is delimited and can easily be separated into columns, as I've had no problem doing in 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    

What I am trying to do is make each activity into a boolean variable which has its own column as such, so it indicates 0 for not having done the activity on that day and 1 for having done the activity. It would look something like this:

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

There are 1 best solutions below

0
milkmaximum On BEST ANSWER

So, what I ended up doing was using my knowledge of Java to reformat data. I first separated the activities into their own variables, each containing a numerical (binary) value to indicate whether or not that activity had been done that day. I had to treat sleep quality separately, so that part looks a little wonky. Here's the code, which produced the correct output:

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