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 trialAnthony Duffy
7,866 PointsCraig, could you post your test cases please? I can't figure out how you are getting "dork" past my code.
I keep getting this error when submitting my code: "Bummer! Whoops looks like you didn't censor the words (2a). mCensoredWords contains 'dork', but it made it past somehow."
this is my output.html: Enter the story template: Please enter a first prompt: Please enter a second prompt: Sorry, but the word dork is not allowed! Please enter a valid second prompt: Sorry, but the word dweeb is not allowed! Please enter a valid second prompt: Your TreeStory:
Testing PASS_1 and then PASS_2
I started out with a really simple while loop (commented out in the attached code) in the Prompter.promptForWord method, but that was failing. I thought maybe you were passing some sneaky dorks to the method e.g. spaces between letters, mixed case, compound words like superdork, etc, so I just quickly wrote up the current (admittedly gross looking) nested loop just to see if I could brute force my way past. Despite not being able to get any form of "dork" I can think of past the loop in my local environment, I still fail in the code checker.
package com.teamtreehouse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
Prompter prompt = new Prompter();
System.out.print("Enter the story template: ");
Template tmpl = null;
try {
tmpl = new Template(prompt.getReader().readLine());
} catch (IOException e) {
e.printStackTrace();
}
prompt.run(tmpl);
try {
prompt.getReader().close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
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 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;
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 yourTreeStory = tmpl.render(results);
System.out.printf("Your TreeStory:%n%n%s", yourTreeStory);
}
/**
* 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: ", phrase);
String response = mReader.readLine();
//while (mCensoredWords.contains(response.toLowerCase())) {
while (true){
boolean rude = false;
for (String item : mCensoredWords) {
if (response.replaceAll("\\s","").toLowerCase().contains(item)) {
System.out.printf("Sorry, but the word %s is not allowed! Please enter a valid %s: ", item, phrase);
response = mReader.readLine();
rude = true;
break;
} else {
rude = false;
}
}
if (!rude)
break;
}
return response;
}
}
# This is essentially what I am testing
1. The user is prompted for a string template (the one with the double underscores in it)
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
3 Answers
Craig Dennis
Treehouse TeacherYikes! I think I see it. Try not printing it out in your warning message. I wasn't expecting that.
Sorry for the frustration! I will tweak it to allow that!
Anthony Duffy
7,866 PointsThat did the job! Thank you, Craig.
james white
78,399 PointsThis was a very frustrating course because the questions
for the challenge were not well phrased to describe what was needed
and some of the error messages were very misleading.
//the line that needed to be changed in Anthony's 'com/teamtreehouse/Prompter.java' code above:
System.out.printf("Sorry, but that word is not allowed! Please enter a valid word: ", item, phrase);
Sławomir Lasik
7,792 PointsYep!! That really should be changed. Especially for the newcomers. That got me like 20 minutes of thinking why my method is not working. Mr. Craig, just warn in the exercise message, that there is a problem with that.
Great Course Though Cheers!
coder5837
1,535 Pointscoder5837
1,535 Pointscould you post your finished code?