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

Hello, I am getting the Bummer: Looks like you did not present Treestory (3) when I did as part of system out print:

code details: package com.teamtreehouse;

import java.io.BufferedReader; import java.io.Console; import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.Charset; import java.nio.file.Path; import java.nio.file.Paths; import java.util.*;

import static java.nio.file.Files.readAllLines;

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;
    Charset charset = Charset.forName("ISO-8859-1");

    try {
        words = readAllLines(file, charset);
    } catch (IOException e) {
        System.out.println("Couldn't load censored words");
        e.printStackTrace();
    }
    mCensoredWords.addAll(words);
}

public void run(Template tmpl) throws IOException {
    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(" Your TreeStory: %n%n%s", tmpl.render(results));
    //  String results = tmpl.render(fakeResults);
    //  System.out.printf("Your TreeStory:%n%n%s", 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()) {
        if(mCensoredWords.contains(phrase)){
            System.out.println("Template contains censored word, re-enter the story template");
            System.exit(0);
        } else {
            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 = null;
    try {
        Scanner readLine = new Scanner(System.in);
        do {
            System.out.println("Enter the " + phrase + ": ");
            response = readLine.next();
            //    System.out.println(response);
        } while (mCensoredWords.contains(response));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return response;
}

public String promptForStory() throws IOException {
    Scanner sreadLine = new Scanner(System.in);
    String story = null;

    //   "Thanks __name__ for helping me out.  You are really a __adjective__. __noun__ and I owe you a __noun__."
    do {
        try {
            System.out.println("Enter the new Story: Enter first sentence with blanks" +
                    "for ex: __name__ ");
            story = sreadLine.nextLine();
            Template firsttempl = new Template(story);
            for (String wrd : firsttempl.getPlaceHolders()) {
                while(mCensoredWords.contains(wrd)||containsCensoredWords(story)) {
                    System.out.println("contains Censored word, re-enter :");
                    story = sreadLine.nextLine();
                }
            }
            System.out.println(" Enter the next sentence: for ex:  __adjective__");
            story = story.concat(". " + sreadLine.nextLine());
            Template secondtempl = new Template(story);
            for (String wrd : secondtempl.getPlaceHolders()) {
                while (mCensoredWords.contains(wrd) || containsCensoredWords(story)) {
                    System.out.println("contains Censored word, re-enter :");
                    story = sreadLine.nextLine();
                }
            }
            System.out.println(" Enter the last line with two blanks: __noun__ __noun__.");
            story = story.concat(". " + sreadLine.nextLine());
            Template thirdtempl = new Template(story);
            for (String wrd : thirdtempl.getPlaceHolders()) {
                while (mCensoredWords.contains(wrd) || containsCensoredWords(story)) {
                    System.out.println("contains Censored word, re-enter :");
                    story = sreadLine.nextLine();
                }
            }
        } catch (Exception excptn) {
            excptn.printStackTrace();
        }

        if (containsCensoredWords(story)) {
            System.out.println("Contains censored word. thanks for playing");
            story= null;
        }
    } while (story == null);
    return story;
}

public boolean containsCensoredWords(String Sentence) {
    String mSentence = Sentence;
    Boolean contains = false;
    try {
        String[] mwords = mSentence.split(" ");
        for (String mword : mwords) {
            if (mCensoredWords.contains(mword)) {
                contains = true;
            }
        }
    } catch (Exception excptn) {
        excptn.printStackTrace();
    }
    System.out.println("Contains Censored words " + contains);
    return contains;
}

}

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

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

public class Main {

    public static void main(String[] args) {


        Prompter prompter = new Prompter();
        Console console = System.console();
        //  String story = "Thanks __name__ for helping me out.  You are really a __adjective__. __noun__ and I owe you a __noun__.";
        String story = null;
        try {
            story = prompter.promptForStory();
          Template tmpl = new Template(story);


                prompter.run(tmpl);
          System.out.printf(" Your TreeStory: %n%n%s", tmpl.render(results));
        } catch (IOException e) {
            e.printStackTrace();
        }

       // if(story!=null) {
         //   Template tmpl = new Template(story);


       //         prompter.run(tmpl);

      //  }
    }
}
com/teamtreehouse/Prompter.java
package com.teamtreehouse;

import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;

import static java.nio.file.Files.readAllLines;


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

   public BufferedReader getReader() {
        return mReader;
    }

    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;
        Charset charset = Charset.forName("ISO-8859-1");

        try {
            words = readAllLines(file, charset);
        } catch (IOException e) {
            System.out.println("Couldn't load censored words");
            e.printStackTrace();
        }
        mCensoredWords.addAll(words);
    }

    public void run(Template tmpl) throws IOException {
        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(" Your TreeStory: %n%n%s", tmpl.render(results));
        //  String results = tmpl.render(fakeResults);
        //  System.out.printf("Your TreeStory:%n%n%s", 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()) {
           // if(mCensoredWords.contains(phrase)){
             //   System.out.println("Template contains censored word, re-enter the story template");
              //  System.exit(0);
            //} else {
                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 = null;
        try {
            Scanner readLine = new Scanner(System.in);
            do {
                System.out.println("Enter the " + phrase + ": ");
                response = readLine.next();
              if (mCensoredWords.contains(response)) {
               System.out.println("That word is not allowed: enter new word");
                 response = null;
              }
                //    System.out.println(response);
            } while (response ==null);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return response;
    }

    public String promptForStory() throws IOException {
        Scanner sreadLine = new Scanner(System.in);
        String story = null;

        //   "Thanks __name__ for helping me out.  You are really a __adjective__. __noun__ and I owe you a __noun__."
        do {
            try {
                System.out.println("Enter the new Treestory Story: using __name__  __adjective __noun__ keywords");
                story = sreadLine.nextLine();
                Template firsttempl = new Template(story);
                for (String wrd : firsttempl.getPlaceHolders()) {
                    while(mCensoredWords.contains(wrd)||containsCensoredWords(story)) {
                        System.out.println("contains Censored word, re-enter :");
                        story = sreadLine.nextLine();
                    }
                }
                /* System.out.println(" Enter the next sentence: with __adjective__ keyword ");
                story = story.concat(". " + sreadLine.nextLine());
                Template secondtempl = new Template(story);
                for (String wrd : secondtempl.getPlaceHolders()) {
                    while (mCensoredWords.contains(wrd) || containsCensoredWords(story)) {
                        System.out.println("contains Censored word, re-enter :");
                        story = sreadLine.nextLine();
                    }
                }
                System.out.println(" Enter the last line with __noun__ and __noun__ keywords ");
                story = story.concat(". " + sreadLine.nextLine());
                Template thirdtempl = new Template(story);
                for (String wrd : thirdtempl.getPlaceHolders()) {
                    while (mCensoredWords.contains(wrd) || containsCensoredWords(story)) {
                        System.out.println("contains Censored word, re-enter :");
                        story = sreadLine.nextLine();
                    }
                } */
            } catch (Exception excptn) {
                excptn.printStackTrace();
            }

            if (containsCensoredWords(story)) {
                System.out.println("Contains censored word. thanks for playing");
                story= null;
            }
        } while (story == null);
        return story;
    }

    public boolean containsCensoredWords(String Sentence) {
        String mSentence = Sentence;
        Boolean contains = false;
        try {
            String[] mwords = mSentence.split(" ");
            for (String mword : mwords) {
                if (mCensoredWords.contains(mword)) {
                    contains = true;
                }
            }
        } catch (Exception excptn) {
            excptn.printStackTrace();
        }
        System.out.println("Contains Censored words " + contains);
        return contains;
    }
}
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

1 Answer

Simon Coates
Simon Coates
28,694 Points

you might want to take a look at this. I think kevin faust's code may work. Might help you isolate a defect.