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

I get an error "Looks like you didn't present the TreeStory(3)"

I run the same code in intelliJ, the result is fine. It can show the completed story. It seems Craig's program tries to check something and I miss out of it, resulting in this error "Looks like you didn't present the TreeStory(3)". Please find my code associated in this discussion.

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("%nYour TreeStory:%n%n%s", tmpl.render(results));
}


public String promptForWord(String phrase) throws IOException {
    String inputtedWord;
    System.out.printf("Please input your word for %s : ", phrase);
    inputtedWord = mReader.readLine().toLowerCase().trim();
    while (inputtedWord.length() == 0 || mCensoredWords.contains(inputtedWord) ) {
        System.out.printf("%nYou didn't input any word or it's censored, please input %s again: ", phrase);
        inputtedWord = mReader.readLine().toLowerCase().trim();
    }
    return inputtedWord;
}

public String promptForStoryTemplate() throws IOException {
    String storyTemplate = mReader.readLine();
    return storyTemplate;
}

Below is Main.java

public class Main {

public static void main(String[] args) {
    // write your code here
    Prompter prompter = new Prompter();
    String story = null;
    System.out.println("Please input your story:  ");
    try {
        story = prompter.promptForStoryTemplate();
    } catch (IOException e) {
        System.out.println("There's a problem inputting a story");
        e.printStackTrace();
        System.exit(0);
    }
    Template tmpl = new Template(story);
    prompter.run(tmpl);

}

}

2 Answers

Seth Kroger
Seth Kroger
56,413 Points

In promptForWord() you don't need to use toLowerCase(). Just let any capitalization go through as-is.

I get passed this challenge. Thank you for helping me out. I owe you a high five.

Luis Garcia
Luis Garcia
1,552 Points

Hey Seth,

Would you mind kinly having a look at mine? It works just fine in IntelliJ

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.println("Enter the story template: ");
        String story = prompter.promptTemplate();
        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;

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

    public String promptTemplate() {
        String template = null;
        try {
            template = mReader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return template;
    }

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

    }

    /**
     * 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 {
        String noun = null;
        Boolean isInvalid = false;
        do {
            System.out.println("Enter replacement:  ");
            noun = mReader.readLine().trim();
            isInvalid = mCensoredWords.contains(noun);
            if (isInvalid)
                System.out.println("I'm sorry, that word is censored. Please try again");

        } while (isInvalid);

        return noun;
    }
}

``
Seth Kroger
Seth Kroger
56,413 Points

I believe you need to include phrase in the output when you ask for a word in promptForWord().

System.out.printf("Enter a %s:  ", phrase);
Luis Garcia
Luis Garcia
1,552 Points

Awesome! That was it!

Thanks a lot, Luis