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 trialDevon Butler
Courses Plus Student 9,030 PointsIntellij gives me good output, but the tester says that I'm not rendering the output.
Really not sure what I'm doing wrong here...
package com.teamtreehouse;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// write your code here
Scanner in = new Scanner(System.in);
// TODO:csd - Instantiate a new Prompter object and prompt for the story template
Prompter prompt = new Prompter();
System.out.printf("Enter new story:%n");
String newStory = in.nextLine();
Template newTemplate = new Template(newStory);
prompt.run(newTemplate);
}
}
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 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(tmpl.render(results));
// TO DO:csd - Print out the results that were gathered here by rendering the template
}
/**
* 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) {
String input = "";
do {
System.out.printf("Please enter a %s: ", phrase);
try {
input = mReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
} while(mCensoredWords.contains(input));
return input;
}
}
# 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
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 PointsHi, Great Job with the code. I see that it works.
You see the problem here is that Craig Dennis
- Assumes that you are using
BufferedReader
and NOTScanner
like used. It is totally find that you use it. It is great. But unfortunately, because initially project files were with Buffered Reader, you have to use it, and notScanner
- Please remove prompting story part to
Prompter
. It is necessary for you to understand that you created Prompter class to make all user prompts there. Seems logical, right :)
So let me give you how the Main
class should look like, and you can try to fill out the missing method I didn't write in Prompter
, OK:)?
So, here is how Main.main
should look like:
public static void main(String[] args) {
// create new Prompter object
Prompter prompt = new Prompter();
// prompt for new story: That is the missing class that
// you have to implement in Prompter
String newStory = prompt.promptForStory();
// create new Template from story
Template newTemplate = new Template(newStory);
// run prompter with template
prompt.run(newTemplate);
}
So go ahead and implement promptForStory()
method using mReader
and NOT Scanner. I'm sure you'll made. Post back here if you'll have more errors.