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 Finishing TreeStory

Teemu Toivonen
Teemu Toivonen
8,741 Points

Local Development Environment - last challenge - last step

After completing the missing parts I get an error while checking the last part of the challenge.

The test states that the program did not present the story to user (3). I have been trying to solve this based on answers on forum and I still find myself to be stuck as while testing on local environment I have not been able to reproduce the results and the story is displayed at the end of the program.

Can you please provide me guidance on how to proceed on correcting my code to fulfill the requirements.

Best Regards, Teemu Toivonen

Main.java
public class Main {

    public static void main(String[] args) {
    // write your code here
        Prompter prompter = new Prompter();
        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
        System.out.printf("%nWrite your story here mark word class for the story with surrounding it with __ (example: __name__) to be filled:%n");
        String story = null;
        try {
            story = buffer.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Template tmpl = new Template(story);
        prompter.run(tmpl);
    }
}
Prompter.java
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));

    }

    /**
     * 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;
    }


    /**
     * 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) throws IOException {
        boolean cencoredWord = false;
        String answer = null;
        while (cencoredWord == false) {
            System.out.println("Enter a response to phrase:  " + phrase);
            answer = mReader.readLine();
            if (mCensoredWords.contains(answer)) {
                System.out.println("Word entered is invalid. Please enter a new word.");
            } else {
                cencoredWord = true;
            }
        }
        return answer;
    }
}

2 Answers

I had an issue with this at first and I believe there is a specific way they want you to word the end result. Try this instead of the println() function:

        String newResults = tmpl.render(results);
        System.out.printf("%nYour TreeStory:%n%n%s", newResults);

Let me know if that works. If that does not solve your issue, I will brain storm some more.

Teemu Toivonen
Teemu Toivonen
8,741 Points

Thank you for your answer, unfortunately it did not resolve the issue.