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

Issue with Finishing TreeStory Java

My code doesn't prompt me back for my variables between the underscores. Can someone help me?

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.printf(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 {

    System.out.printf("Please enter your word for %s",phrase);
    String entryWord = mReader.readLine();

    while(mCensoredWords.contains(entryWord.toLowerCase().trim())){
        System.out.printf("Invalid word. It is rude to use %s",entryWord);
        entryWord = mReader.readLine();
    }
    return entryWord.trim();

}

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

}

public static void main(String[] args) {
// write your code here

    Prompter prompter = new Prompter();
    BufferedReader mReader = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Make a story! Write a fill in the blank story with an underscore on each side of the variable like _noun/adjective/etc_");
    String story = null;
    try {
        story = prompter.promptForStory();
    }   catch (IOException ioe) {
        ioe.printStackTrace();
    }
    Template tmpl = new Template(story);
    prompter.run(tmpl);

}

}

1 Answer

Nothing is jumping out at me code-wise but I believe the phrases should be surrounded by double underscores, not single, on each side.

What do you mean by double underscores? Is it in the pattern file or the input in the console

The phrases should be surrounded by two underscores on either side because that is what Template.java, which you aren't supposed to touch for this exercise, is looking for. So if you want a noun, you would type __noun__, not _noun_.

Oh my gosh, thank you so much. I feel so dumb now. :D