Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Java

I'm lost and would greatly appreciate someone guiding me through the Teamwork Challenge

Hey Craig Dennis ,

I feel like after all these lessons I've finally been thrown into the deep-end and have to figure out how to swim haha. It's just I'm a bit overwhelmed by this whole IDE thing. I went back to Objects and Data Structures repeated 'em and came back here but I still feel a little lost when I look at the code on IntelliJ.

Would really appreciate anyone who can guide me with the TODO's , the task is 'Teamwork' after all :p Grigorij Schleifer , Steve Hunter :D

Cheers, Christiaan

Hi Christiaan,

Which challenge is this - do you have a link to it?

Steve.

Hey Steve,

I think this is the challenge - Finishing TreeStory.

Am I right, Christiaan?

2 Answers

Hi Christian,

I know your feeling. I repeated every course twice, so I know what you are talking about. Don´t give up !

Let us look at my code that has worked for me.

Main.java

package com.teamtreehouse;

import java.io.IOException;

public class Main {

    public static void main(String[] args) {
        Prompter prompter = new Prompter();
// create a new Prompter object
        String story = "";
// create a story String and initialize it to " "
// there is no story yet, but the Users story-template will be stored inside "story"

        try {
            story = prompter.promptForStory();
// inside the try block use the promptForStory() method from the Prompter class
// to ask the User for a Story Template and "__wordsThatWillBeReplaced__ "
// this method uses the BufferedReader called mReader that is created inside the Prompter class
// using readLine method you will get a Story as a String object and store it inside "story"

        } catch (IOException ioe) {
// catch exception if something is wrong
            ioe.printStackTrace();
        }
        Template tmpl = new Template(story);
// give the story for analysis to the Template class
// inside the Template class the "__wordsThatWillBeReplaced__" inside the story will be analized, extracted
// and stored inside the List called mPlaceHolders (see Template class)
        prompter.run(tmpl);
// look at the run method inside the Prompter class
// the run method accepts the template that is created inside the Template class
// the promtForWords method uses a for loop and gets the placeHolders (____wordsThatWillBeReplaced____) 
// from the Template class and stores it inside the String "phrase"
// this "phrase" is handled to the promptForWord method that presents the placeHolders to the User 
// and asks him to input the proper words and replace the placeHolder ( __ ? __ )
// if the Users input is not censored a trimed answer will be returned
    }
}

Prompter.java

package com.teamtreehouse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


public class Prompter {
    private BufferedReader mReader;
    private Set<String> mCensoredWords;

    public Prompter() {
        mReader = new BufferedReader(new InputStreamReader(System.in));
        loadCensoredWords();
    }

    private void loadCensoredWords() {
        mCensoredWords = new HashSet<String>();
        Path file = Paths.get("resources", "censored_words.txt");
        List<String> words = null;
        try {
            words = Files.readAllLines(file);
        } catch (IOException e) {
            System.out.println("Couldn't load censored words");
            e.printStackTrace();
        }
        mCensoredWords.addAll(words);
    }

    public void run(Template tmpl) {
        List<String> results = null;
        try {
            results = promptForWords(tmpl);
        } catch (IOException e) {
            System.out.println("There was a problem prompting for words");
            e.printStackTrace();
            System.exit(0);
        }

        System.out.println(tmpl.render(results));

    }


    public List<String> promptForWords(Template tmpl) throws IOException {
        List<String> words = new ArrayList<String>();
        for (String phrase : tmpl.getPlaceHolders()) {
            String word = promptForWord(phrase);
            words.add(word);
        }
        return words;
    }



    public String promptForWord(String phrase) throws IOException {
        System.out.printf("Please Enter your words for %s:: %n", phrase);
        String answer = mReader.readLine();

        while (mCensoredWords.contains(answer)) {
            System.out.printf("Sorry %s is an unacceptable word%n", phrase);
            answer = mReader.readLine();

        }
        return answer.trim();
    }


    public String promptForStory() throws IOException {
        return mReader.readLine();
    }
}

Template.class

package com.teamtreehouse;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Template {
    private String mCompiled;
    private List<String> mPlaceholders;

    public Template(String text) {
        Pattern pattern = Pattern.compile("__([^__]+)__");
        Matcher matcher = pattern.matcher(text);
        this.mPlaceholders = new ArrayList();

        while(matcher.find()) {
            String label = matcher.group(1);
            this.mPlaceholders.add(label);
        }

        this.mCompiled = matcher.replaceAll("%s");
    }

    public List<String> getPlaceHolders() {
        return this.mPlaceholders;
    }

    public String render(List<String> values) {
        return String.format(this.mCompiled, values.toArray());
    }
}

This is my logic for the challenge. Don´t hesitate to ask if something isn´t clear.

Grigorij

Yes that is the challenge ! Oh man I knew I couldn't be the only one who understands things better with repetition haha and it's so rewarding when you finally do understand concepts that didn't make sense despite you moving on. I can't thank you enough Grigorij ! I'll go through this in-depth and get back to you if I have anything , thanks again :D

Hey Christiaan,

take your time. The community will always help you. Just give us a shout :smiley:

Hi Steve Hunter , thanks for getting back .. here's the challenge -

https://teamtreehouse.com/library/local-development-environments/advanced-tooling/finishing-treestory

'Finishing TreeStory' in the local development environments course.