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 Local Development Environments Advanced Tooling Teamwork

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 Points

I completed this challenge. Any feedback?

This passes the tests (the challenge after this video), I'm just looking for any additional feedback on my code.

main.java

package com.teamtreehouse;

import java.util.Arrays;
import java.util.List;

public class Main {

    public static void main(String[] args) {
    // write your code here
        Prompter prompter = new Prompter();
        System.out.printf("Please give me a story, with placeholders in between double underscores%n");
        System.out.printf("(e.g. '__name__', '__adjective__', '__noun__'): %n");
        String story = prompter.promptForStory();
        Template tmpl = new Template(story);
        prompter.run(tmpl);
    }
}

prompter.java

package com.teamtreehouse;
import java.io.*;
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;
    private String mStory;

    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> words = null;
        try {
            words = promptForWords(tmpl);
        } catch (IOException e) {
            System.out.println("There was a problem prompting for words");
            e.printStackTrace();
            System.exit(0);
        }
        String results = tmpl.render(words);
        System.out.printf("Your TreeStory:%n%n%s", results);
    }

    /**
     * Prompts user for each of the blanks
     *
     * @param tmpl The compiled template
     * @return
     * @throws IOException
     */
    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 promptForStory() {
        String story = null;
        try {
            story = mReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return story;
    }

    /**
     * Prompts the user for the answer to the fill in the blank.  Value is guaranteed to be not in the censored words list.
     *
     * @param phrase The word that the user should be prompted.  eg: adjective, proper noun, name
     * @return What the user responded
     */
    public String promptForWord(String phrase) {
        String word = null;
        System.out.printf("Give me a(n) %s: ", phrase);

        try {
            word = mReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (mCensoredWords.contains(word)) {
            System.out.printf("That's a censored word, try again.%n");
            word = promptForWord(phrase);
        }

        return word;
    }
}

censored_words.txt

dork
jerk
nerd
dweeb

template.java

package com.teamtreehouse;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Template {
    /**
     * The string format that is waiting to receive values
     */
    private String mCompiled;
    private List<String> mPlaceholders;

    /**
     * @param text A template with double underscored surrounded placeholders. eg: Hello __name__!
     */
    public Template(String text) {
        // Match on double underscore surrounded words, like __name__ or __proper noun__
        Pattern pattern = Pattern.compile("__([^__]+)__");
        Matcher matcher = pattern.matcher(text);
        mPlaceholders = new ArrayList<String>();
        while (matcher.find()) {
            String label = matcher.group(1);
            mPlaceholders.add(label);
        }
        mCompiled = matcher.replaceAll("%s");
    }


    /**
     * @return Ordered names of placeholders in the template
     */
    public List<String> getPlaceHolders() {
        return mPlaceholders;
    }


    /**
     * Given a list of values, replaces the fill in the blanks in order.
     *
     * @param values The replacements for the fill in the blank
     * @return The filled out TreeStory
     */
    public String render(List<String> values) {
        // String.format accepts the templates and Object... (a variable amount of objects)
        return String.format(mCompiled, values.toArray());
    }
}

4 Answers

Roman Mayer
Roman Mayer
10,925 Points

Main: You can clean imports. Don't need any. You can move the two printf's in the Prompter method.

Prompter: In promptForStory, if you get an IOException, you won't enter the try-block and return null. Might cause problems. In promptForWord, you've got to use a loop. With your implementation, I can enter a bad word, you ask me to try again, I enter the same word and you don't check for bad_word again.

    public String promptForWord(String phrase) throws IOException {
        String word;
        System.out.printf("Please enter the %s:  ", phrase);
        boolean goodResponse = false;
        do {
            word = mReader.readLine();
            if (!mCensoredWords.contains(word)) {
                goodResponse = true;
            }
        } while (!goodResponse);
        return word;
    }

Where did your

List<String> fakeResults = Arrays.asList(
                "friend",
                "talented",
                "java programmer",
                "high five");

from Main go?

Oh man thank you so much for this code. It helped me somehow pass but I still can not figure out why my code didn't work. I tried to change so many things but without success :/

This is the post I created hoping someone could find a fix. https://teamtreehouse.com/community/cant-pass-the-last-challenge-i-tried-for-a-while-but-i-just-cant-figure-it-out

The issue is that my tests work fine on the IDE. I tried copying parts of your code over mine where I found it different but that didn't fix it on the challenge. I kept getting the error "You did not show the story(3)". For me it only worked as soon as I used your code fully.

It would really help if you could tell me what exactly is the issue since it works fine for me when I test it.