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
kevinloader
6,825 PointsBummer! Whoops looks like you didn't censor the words (2a). mCensoredWords contains 'dork', but it made it past somehow.
Okay, I was having problems getting my story finished. I tried everything but Treehouse would not take it. Here is my original code
package com.teamtreehouse;
public class Main {
public static void main(String[] args) {
Prompter prompt = new Prompter();
String story = prompt.promptForStory();
Template tmpl = new Template(story);
prompt.run(tmpl);
}
}
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.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 void run(Template tmpl) {
List<String> results = null;
try {
results = promptForWords(tmpl);
} catch (IOException ioe) {
System.out.println("There was a problem prompting for words");
ioe.printStackTrace();
System.exit(0);
}
String newResults = tmpl.render(results);
System.out.printf("Your TreeStory:%n%n%s", newResults);
}
/**
* 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;
}
public String promptForStory() {
//System.out.printf("Example: Thanks __name__ for helping me out. You are really a __adjective__ __noun__ and I owe you a __noun__.%n");
System.out.printf("Make sure name, adjective and noun have 2 __ before and after.%n");
System.out.printf("Enter a new Story: ");
String story = null;
try {
story = mReader.readLine();
} catch (IOException ioe) {
System.out.println("There was a problem with prompting for story");
ioe.printStackTrace();
System.exit(0);
}
return story;
}
/**
* 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) {
String word = null;
do {
System.out.printf("Enter a new '%s' Word to complete Story: ", phrase);
try {
word = mReader.readLine();
} catch (IOException ioe) {
System.out.println("There was a problem with prompting for word%n");
ioe.printStackTrace();
System.exit(0);
}
if (mCensoredWords.contains(word.toLowerCase())) {
System.out.printf("You entered a censored word '%s' Please try again%n%n", word);
}
} while (mCensoredWords.contains(word.toLowerCase()));
return word;
}
}
I could not get past this so I tried this (removing the System.out): if (mCensoredWords.contains(word.toLowerCase())) { //System.out.printf("You entered a censored word '%s' Please try again%n%n", word); }
The minute I did this, I was able to pass the course.
Craig, please fix, thanks Kevin.
2 Answers
Craig Dennis
Treehouse TeacherIt's hard, I'm looking at the output of your program and your output says the censored word. I'll try to come up with a different solution.
If I changed the instructions to say "You must not display the censored word" Do you think that would work?
Sorry about the frustration :/
kevinloader
6,825 PointsSure, I put it there for clarity to show the user that they entered an invalid word. But if the instructions were just to state that "You must not display the censored word" that should be clear enough. Thanks for the quick reply.