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

Keep on getting 'Bummer! Looks like you didn't present the TreeStory (3)' message

Not sure what I am messing up here, but I keep on getting "Bummer! Looks like you didn't present the TreeStory (3)" for the last challenge of the Local Development Environments stream. Am I missing something in my code?

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 = null;
    try {
        story = prompter.promptForTemplate();
    } catch (IOException ioe) {
        ioe.printStackTrace();
        System.exit(0);
    }
    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();
}

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 String promptForTemplate() throws IOException {
    System.out.print("Please enter your story template: ");
    String template = mReader.readLine();
    return template;
}

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("%n%nYour Treestory: %s", 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 {
    String response = "";
    System.out.printf("Please provide a %s: ", phrase);
    response = mReader.readLine().toLowerCase().trim();

    while(mCensoredWords.contains(response)) {
        System.out.printf("Please be nice! Try again.%n");
        System.out.printf("Please provide a %s: ", phrase);
        response = mReader.readLine().toLowerCase().trim();
    }
    return response;
}

}

P.S. Sorry if this looks messy... it's late and I am very tired :)

3 Answers

The error was very hard to find, but I think I did it. The problem is, when you prompt user for response in promptForWord method, use change his response to be trimmed and lowercased. When Craigs tests checking you TreeStory they don't match. Do you see the problem. Try to change your promptForWord method to this one:

public String promptForWord(String phrase) throws IOException {
    String response = "";
    System.out.printf("Please provide a %s: ", phrase);
    // response = mReader.readLine().toLowerCase().trim(); // wrong
    response = mReader.readLine(); // correct

    while(mCensoredWords.contains(response)) {
        System.out.printf("Please be nice! Try again.%n");
        System.out.printf("Please provide a %s: ", phrase);
        // response = mReader.readLine().toLowerCase().trim(); // wrong
        response = mReader.readLine(); // correct
    }
    return response;
}

One more advice though: When you have problems with the challenge. Try to push button 'Get Help'. It won't work after you pushed 'Check' and have errors - it is a bug in treehouse. But If you just put your code and then before compiling press Get Help, it should automatically include your code to question in community.

Otherwise try to use preview button and take a look at your response before you send it (Take a look at Markdown cheatsheet hint).

You know: the easier to check your error: copy/paste code, the faster people will help you. And also feel free to edit your post. If you posted it late at night, and no one answered feel free to comeback next day and format it properly. Even on StackOverflow people edit their posts :)

hello friends. Looks like you didn't present the TreeStory (3) is the error that Iam geting when I submit my code. I dont have any errors in InteliJ. this is the code:

package com.teamtreehouse;

import java.io.*; import java.nio.charset.Charset; 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, Charset.defaultCharset());
    } 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);
    }

    for (String s : results){
        System.out.print(s + "");
    }
}


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



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

    response = mReader.readLine();

    while(mCensoredWords.contains(response)) {
        System.out.printf("Please be nice! Try again.%n");
        System.out.printf("Please provide a %s: ", phrase);
        response = mReader.readLine();
    }
    return response;
}




public String promptForTemplate() throws IOException {
    System.out.print("Please enter your story template: ");
    String template = mReader.readLine();
    return template;
}

}

package com.teamtreehouse;

import java.io.IOException; import java.util.Arrays; import java.util.List;

public class Main {

public static void main(String[] args) {

    Prompter prompter = new Prompter();

    String story = null;

    try {
        story=prompter.promptForTemplate();

    }catch (IOException ioe){

        ioe.printStackTrace();
        System.exit(0);
    }

    Template tmpl = new Template(story);
    prompter.run(tmpl);


}

}

It worked with your suggestion; thank you Alex! Also thanks for the additional tips :)

Ninoslav Bozilovic

Test number three check that your treestory is properly printed. Method printing your treestory is:

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);
    }

    // change this method. the error is very easy. Hint take a look how Craig prints treestory. in the given files
    // the way you do it is incorrect.
    // for (String s : results){
    //    System.out.print(s + "");
    //}
}