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

Looks like you didn't present the TreeStory(3).

Hello All,

I completed my code and it runs via IntelliJ. I even see the output if I "preview" my code but the challenge is still telling me I didn't present the TreeStory. I am assuming the 3 refers to the third pseudo-test which says to present the finished story to the user. But the preview of the output even shows "Here is your complete story: ##story the test created##" So I'm not sure where I've gone wrong. Any help would be appreciated!

Main.java

package com.teamtreehouse;

import java.io.IOException;

public class Main {

    public static void main(String[] args) {

        Prompter prompter = new Prompter();
        System.out.println("Write a fun story! Create a fill in the blank story with two underscores on each side of the variable like __noun__ or __adjective__!");
        String userInput = null;
        String defaultStory = "Thanks __name__ for helping me out. You are really a __adjective__ __noun__ and I owe you a __noun__.";
        try {
            userInput = prompter.promptForStory();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (userInput.contains("__")) {
            Template userTemplate = new Template(userInput);
            prompter.run(userTemplate);
        } else {
            Template defaultTemplate = new Template(defaultStory);
            prompter.run(defaultTemplate);
        }
    }
}

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.*;


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

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

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

    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 completedStory = tmpl.render((results));
        System.out.println(completedStory);
    }

    /**
     * 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 a %s. %n", phrase);

        String response = mReader.readLine();
            while(mCensoredWords.contains(response.toLowerCase().trim())) {
                System.out.printf("That word isn't very nice. Please enter a new word for %s. %n%n", phrase);
                response = mReader.readLine();
            }
            String enteredWord = response.toLowerCase().trim();

        return enteredWord;
    }
}

What I see when I run the preview in the code challenge

Write a fun story! Create a fill in the blank story with two underscores on each side of the variable like __noun__ or __adjective__!
Please enter a first prompt. 
Please enter a second prompt. 
That word isn't very nice. Please enter a new word for second prompt. 

That word isn't very nice. Please enter a new word for second prompt. 

Here is your completed story: 
 Testing pass_1 and then pass_2

1 Answer

I figured out my problem... I was returning the prompt for word as lowercase. The challenge checker thing didn't like that. I changed the return in my promptForWord method to return response.trim(); instead of return response.toLowerCase().trim(); and that resolved it. Frustrating that the description of the "error" couldn't be more helpful.

Thank you, Jen. This was driving me crazy!