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

T Essex
T Essex
35,288 Points

"Bummer: Looks like you didn't present the TreeStory (3)" error despite output showing in terminal

I can't get the above test to pass, despite having a call to System.out.printf("Your TreeStory:%n%n%s", output) in the Prompter.run method that outputs the result of running the program to the terminal in my IDE.

I think this must be a bug, as my code just calls the Prompter.render method like the original code with the hard coded story and displays exactly the same output if the original story is used as input.

com/teamtreehouse/Main.java
package com.teamtreehouse;

import java.io.IOException;
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();
        String story = "";
        try {
            story = prompter.promptForStory();
        } catch (IOException ioe) {
            System.out.println("Error with story!");
        }
        Template tmpl = new Template(story);
        prompter.run(tmpl);
    }
}
com/teamtreehouse/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;

    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);
        }
        String output = tmpl.render(results);
        System.out.printf("Your TreeStory:%n%n%s", output);
    }

    /**
     * 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 {
        System.out.printf("Please respond: %s", phrase);
        String response = mReader.readLine();
        for (String censoredWord : mCensoredWords) {
            if (response.contains(censoredWord)) {
                System.out.printf("No, that is rude. Please respond nicely: %s", phrase);
                return mReader.readLine();
            }
        }

        return response;
    }

    public String promptForStory() throws IOException {
        System.out.println("Please enter a story template");
        return mReader.readLine();
    }
}
pseudo-tests.md
#  This is essentially what I am testing 
1.  The user is prompted for a new string template (the one with the double underscores in it).

  a. The prompter class has a new method that prompts for the story template, and that method is called.

2.  The user is then prompted for each word that has been double underscored.

   a. The answer is checked to see if it is contained in the censored words.
      User is continually prompted until they enter a valid word

3.  The user is presented with the completed story

2 Answers

Gergely Horvath
PLUS
Gergely Horvath
Courses Plus Student 8,207 Points

Look at your promptForWord method:

  1. you prompt for word
  2. you loop through censored words to check
  3. if word (or its substring) is censored then you return the result of a new readline which also can contain a censored word, but it is never tested
  4. if word (or its substring) is not censored you return it
T Essex
T Essex
35,288 Points

Thanks Gergely for taking the time to look at this, that was really helpful and it's appreciated.

I fixed the method with a while loop so it continues to check and prompt correctly when a censored word is entered and now my code is passing.

For anyone else who is getting a similar error, it seems that the error message given can be a bit misleading - if you are getting an error from a part of your code that looks correct, go through and check all the other tests and not just the one that the error message references.

This is the corrected code snippet in case anyone is interested:

public String promptForWord(String phrase) throws IOException {
        System.out.printf("Please enter a word (%s): ", phrase);
        String response = mReader.readLine();

        while (stringSetContainsString(mCensoredWords, response)) {
            System.out.printf("That is rude! Please respond again with better language (%s): ", phrase);
            response = mReader.readLine();
        }

        return response;
    }

    private boolean stringSetContainsString(Set<String> set, String string) {
        for (String item : set) {
            if (item.contains(string)) return true;
        }
        return false;
    }