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 trialcoder5837
1,535 Pointsplease help me finish this challenge!
can someone please just help me finish this? I don't want any hints or anything I just want the answer so I can FINALLY finish this course please
package com.teamtreehouse;
import java.io.BufferedReader;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
String story = "Thanks __name__ for helping me out. You are really a __adjective__ __noun__ and I owe you a __noun__.";
Template tmpl = new Template(story);
Prompter prompter = new Prompter();
prompter.run(tmpl);
}
}
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);
}
}
/**
* 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) {
return "";
}
public String promptForStoryTemplate() {
System.out.printf("Enter the story: ");
String line = null;
try {
line = mReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
}
# 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
coder5837
1,535 Pointscan someone please just post the finished code! I am so frustrated right now I have been working for hours on end and I am literally sobbing at my computer can someone please just help me
2 Answers
Craig Dennis
Treehouse TeacherIf you do not understand a TODO please explain what you don't understand. As you know, I will not just post an answer. Since I do not know what your question is, I can't answer it.
Emily this is no different than coding in the real world, so I ask that you attempt to embrace the situation this code challenge puts you into. It's literally the reason I wrote it this way.
Ask specific questions to get yourself unblocked.
coder5837
1,535 Pointscan you PLEASE JUST POST THE FINISHED CODE
coder5837
1,535 Pointsmy specific question is: How exactly to I prompt for an object, use the prompter object for it to prompt, censor and output, how do I get the implementation details out of the main method, print out the results gathered by rendering the template, and how to prompt the user for the respond to the phrase. And yes I did watch the video which was ABSOLUTELY AND COMPLETELY UNHELPFUL
Craig Dennis
Treehouse TeacherHave you done prompting in any of the other pre requisites to this course?
Craig Dennis
Treehouse TeacherThe karaoke machine in this course prompts.
coder5837
1,535 Pointsyeah i finished all of the previous courses
coder5837
1,535 Pointsthen what would read the line
coder5837
1,535 Pointsfor the first one what would read the line this is what I have so far Prompter prompter = new Prompter(); System.out.println("Enter your story: "); return .readLine();
coder5837
1,535 PointsI have absolutely no idea what to do for the second one
Craig Dennis
Treehouse TeacherIn your code you already have promptForStoryTemplate
. That needs to be called in Main
.
coder5837
1,535 Pointsare you talking about the first to do or the second one?
Craig Dennis
Treehouse TeacherYou have 1a but you need 1.
coder5837
1,535 Pointswhat is 1a? Can you at least post what I'm supposed to do for the first todo?
coder5837
1,535 Pointswhat is 1a? Can you at least post what I'm supposed to do for the first todo?
Craig Dennis
Treehouse TeacherLook at this forum post. Can you see 1a?
coder5837
1,535 PointsI don't have 1a yet what reads the line?
coder5837
1,535 PointsI want to be done already I honestly don't care about understanding it I just want closure before quitting treehouse for the summer can you please just post the finished code? Tomorrow I'm going on vacation and I would really like to not worry about it while I am there could you please just post the code so I can be sane at mind and just relax?
Craig Dennis
Treehouse TeacherLiterally copied and pasted from the forum post that you posted:
a. The prompter class has a new method that prompts for the story template, and that method is called.
coder5837
1,535 PointsI don't understand how that helps?
Craig Dennis
Treehouse Teacherthat method is called.
Is that method called?
coder5837
1,535 Pointspackage com.teamtreehouse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
Prompter prompter = new Prompter();
prompter.getmReader;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter the story template: ");
String story = null;
try {
story = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
Template tmpl = new Template(story);
prompter.run(tmpl);
}
}
for prompter:
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 e) {
System.out.println("There was a problem prompting for words");
e.printStackTrace();
System.exit(0);
}
String story = tmpl.render(results);
System.out.printf("%nYour TreeStory:%n%n%s", story);
}
/**
* 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 BufferedReader getmReader() {
return mReader;
}
public String promptForWord(String phrase) {
String word = null;
while(mCensoredWords.contains(word) || word == null) {
try {
System.out.printf("Please enter a %s: ", phrase);
word = mReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if(mCensoredWords.contains(word)){
System.out.print("Oops! That word is censored. Please enter another word: ");
}
}
return word;
}
}
how is this code looking?
Craig Dennis
Treehouse TeacherPretty good...
in your original code you had:
public String promptForStoryTemplate() {
System.out.printf("Enter the story: ");
String line = null;
try {
line = mReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
You don't need to expose the reader or import it.
You need to call:
String story = prompter.promptForStoryTemplate();
coder5837
1,535 Pointsi tried that but I'm still getting this error? ./com/teamtreehouse/Main.java:11: error: cannot find symbol String story = prompter.promptForStoryTemplate(); ^ symbol: method promptForStoryTemplate() location: variable prompter of type Prompter ./com/teamtreehouse/Main.java:15: error: variable story is already defined in method main(String[]) String story = null; ^ 2 errors
coder5837
1,535 Pointswait a minute I'm getting this error now ./com/teamtreehouse/Main.java:15: error: cannot find symbol String story = prompter.promptForStoryTemplate(); ^ symbol: method promptForStoryTemplate() location: variable prompter of type Prompter 1 error
Craig Dennis
Treehouse TeacherDo you have the order right? Remember you can't use something before declaring it.
coder5837
1,535 PointsI tried playing with the order but it keeps on saying that one phrase or the other has an issue: this is what I have right now:
package com.teamtreehouse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
Prompter prompter = new Prompter();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter the story template: ");
try {
story = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
String story = prompter.promptForStoryTemplate();
Template tmpl = new Template(story);
prompter.run(tmpl);
}
}
Craig Dennis
Treehouse TeacherDelete this:
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter the story template: ");
try {
story = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
(If you surround your code in 3 back ticks ``` and write java it will color code)
coder5837
1,535 PointsI just did that and the same error appeared:
./com/teamtreehouse/Main.java:14: error: cannot find symbol
String story = prompter.promptForStoryTemplate();
^
symbol: method promptForStoryTemplate()
location: variable prompter of type Prompter
1 error
coder5837
1,535 Pointsthisi is my code as of now:
package com.teamtreehouse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) {
Prompter prompter = new Prompter();
String story = prompter.promptForStoryTemplate();
Template tmpl = new Template(story);
prompter.run(tmpl);
}
}
Craig Dennis
Treehouse TeacherLooks good...does it run in IntelliJ?
Craig Dennis
Treehouse TeacherDid you re-add that promptForStoryTemplate
that you originally had?
coder5837
1,535 Pointsit doesn't run in the code challenge though this is my error: ./com/teamtreehouse/Main.java:14: error: cannot find symbol String story = prompter.promptForStoryTemplate(); ^ symbol: method promptForStoryTemplate() location: variable prompter of type Prompter 1 error
coder5837
1,535 Pointseven though as you can see in the code, I already defined prompter beforehand?
Craig Dennis
Treehouse TeacherAre you sure you have that method locally and on the code challenge version?
Craig Dennis
Treehouse TeacherThe error is talking about:
symbol: method promptForStoryTemplate()
coder5837
1,535 Pointswait a minute this is what I have for the Prompter.java: what am I doing wrong?
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 e) {
System.out.println("There was a problem prompting for words");
e.printStackTrace();
System.exit(0);
}
String story = tmpl.render(results);
System.out.printf("%nYour TreeStory:%n%n%s", story);
}
/**
* 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 BufferedReader getmReader() {
return mReader;
}
public String promptForWord(String phrase) {
String word = null;
while(mCensoredWords.contains(word) || word == null) {
try {
System.out.printf("Please enter a %s: ", phrase);
word = mReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if(mCensoredWords.contains(word)){
System.out.print("Oops! That word is censored. Please enter another word: ");
}
}
return word;
}
}
Craig Dennis
Treehouse TeacherYour original code contained a new method which I had assumed you still had in there.
public String promptForStoryTemplate() {
System.out.printf("Enter the story: ");
String line = null;
try {
line = mReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
Craig Dennis
Treehouse TeacherYour original code contained a new method which I had assumed you still had in there.
public String promptForStoryTemplate() {
System.out.printf("Enter the story: ");
String line = null;
try {
line = mReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
coder5837
1,535 Pointsokay there is definitely something wrong with that method though because I changed my code and tacked it back onto the end:
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 e) {
System.out.println("There was a problem prompting for words");
e.printStackTrace();
System.exit(0);
}
String story = tmpl.render(results);
System.out.printf("%nYour TreeStory:%n%n%s", story);
}
/**
* 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 BufferedReader getmReader() {
return mReader;
}
public String promptForWord(String phrase) {
String word = null;
while(mCensoredWords.contains(word) || word == null) {
try {
System.out.printf("Please enter a %s: ", phrase);
word = mReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if(mCensoredWords.contains(word)){
System.out.print("Oops! That word is censored. Please enter another word: ");
}
}
return word;
public String promptForStoryTemplate() {
System.out.printf("Enter the story: ");
String line = null;
try {
line = mReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
}
}
but I am still getting this error:
./com/teamtreehouse/Prompter.java:101: error: illegal start of expression
public String promptForStoryTemplate() {
^
./com/teamtreehouse/Prompter.java:101: error: ';' expected
public String promptForStoryTemplate() {
^
./com/teamtreehouse/Main.java:14: error: cannot find symbol
String story = prompter.promptForStoryTemplate();
^
symbol: method promptForStoryTemplate()
location: variable prompter of type Prompter
3 errors
Craig Dennis
Treehouse TeacherCheck your curly braces. Try Code > Reformat code in IntelliJ if you don't see it.
coder5837
1,535 PointsI feel like I'm going in circles. I am only getting more and more errors. this is my code after I refactored it:
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 e) {
System.out.println("There was a problem prompting for words");
e.printStackTrace();
System.exit(0);
}
String story = tmpl.render(results);
System.out.printf("%nYour TreeStory:%n%n%s", story);
}
/**
* 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 BufferedReader getmReader() {
return mReader;
}
public String promptForWord(String phrase) {
String word = null;
while (mCensoredWords.contains(word) || word == null) {
try {
System.out.printf("Please enter a %s: ", phrase);
word = mReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (mCensoredWords.contains(word)) {
System.out.print("Oops! That word is censored. Please enter another word: ");
}
}
return word;
public String promptForStoryTemplate() {
System.out.printf("Enter the story: ");
String line = null;
try {
line = mReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
}
}
and I have more errors than before:
./com/teamtreehouse/Prompter.java:99: error: illegal start of expression
public String promptForStoryTemplate() {
^
./com/teamtreehouse/Prompter.java:99: error: ';' expected
public String promptForStoryTemplate() {
^
./com/teamtreehouse/Prompter.java:119: error: class, interface, or enum expected
public BufferedReader getmReader() {
^
./com/teamtreehouse/Prompter.java:121: error: class, interface, or enum expected
}
^
./com/teamtreehouse/Prompter.java:123: error: class, interface, or enum expected
public String promptForWord(String phrase) {
^
./com/teamtreehouse/Prompter.java:126: error: class, interface, or enum expected
while (mCensoredWords.contains(word) || word == null) {
^
./com/teamtreehouse/Prompter.java:129: error: class, interface, or enum expected
word = mReader.readLine();
^
./com/teamtreehouse/Prompter.java:130: error: class, interface, or enum expected
} catch (IOException e) {
^
./com/teamtreehouse/Prompter.java:132: error: class, interface, or enum expected
}
^
./com/teamtreehouse/Prompter.java:136: error: class, interface, or enum expected
}
^
./com/teamtreehouse/Prompter.java:141: error: class, interface, or enum expected
public String promptForStoryTemplate() {
^
./com/teamtreehouse/Prompter.java:143: error: class, interface, or enum expected
String line = null;
^
./com/teamtreehouse/Prompter.java:144: error: class, interface, or enum expected
try {
^
./com/teamtreehouse/Prompter.java:146: error: class, interface, or enum expected
} catch (IOException e) {
^
./com/teamtreehouse/Prompter.java:148: error: class, interface, or enum expected
}
^
./com/teamtreehouse/Prompter.java:150: error: class, interface, or enum expected
}
^
./com/teamtreehouse/Main.java:14: error: cannot find symbol
String story = prompter.promptForStoryTemplate();
^
symbol: method promptForStoryTemplate()
location: variable prompter of type Prompter
17 errors
Craig Dennis
Treehouse TeacherSomething missing here?
return word;
public String promptForStoryTemplate() {
Craig Dennis
Treehouse TeacherRemember when I said: "Check your curly braces. "
coder5837
1,535 PointsI did remove the curly brace but that is not the only issue because I keep on getting issues for the prompt method. ./com/teamtreehouse/Prompter.java:99: error: illegal start of expression public String promptForStoryTemplate() { ^ ./com/teamtreehouse/Prompter.java:99: error: ';' expected public String promptForStoryTemplate() { ^ ./com/teamtreehouse/Prompter.java:119: error: class, interface, or enum expected public BufferedReader getmReader() { ^ ./com/teamtreehouse/Prompter.java:121: error: class, interface, or enum expected } ^ ./com/teamtreehouse/Prompter.java:123: error: class, interface, or enum expected public String promptForWord(String phrase) { ^ ./com/teamtreehouse/Prompter.java:126: error: class, interface, or enum expected while (mCensoredWords.contains(word) || word == null) { ^ ./com/teamtreehouse/Prompter.java:129: error: class, interface, or enum expected word = mReader.readLine(); ^ ./com/teamtreehouse/Prompter.java:130: error: class, interface, or enum expected } catch (IOException e) { ^ ./com/teamtreehouse/Prompter.java:132: error: class, interface, or enum expected } ^ ./com/teamtreehouse/Prompter.java:136: error: class, interface, or enum expected } ^ ./com/teamtreehouse/Prompter.java:140: error: class, interface, or enum expected } ^ ./com/teamtreehouse/Prompter.java:142: error: class, interface, or enum expected public String promptForStoryTemplate() { ^ ./com/teamtreehouse/Prompter.java:144: error: class, interface, or enum expected String line = null; ^ ./com/teamtreehouse/Prompter.java:145: error: class, interface, or enum expected try { ^ ./com/teamtreehouse/Prompter.java:147: error: class, interface, or enum expected } catch (IOException e) { ^ ./com/teamtreehouse/Prompter.java:149: error: class, interface, or enum expected } ^ ./com/teamtreehouse/Prompter.java:151: error: class, interface, or enum expected } ^ ./com/teamtreehouse/Main.java:14: error: cannot find symbol String story = prompter.promptForStoryTemplate(); ^ symbol: method promptForStoryTemplate() location: variable prompter of type Prompter 18 errors
Craig Dennis
Treehouse TeacherWithout the ending curly brace in promptForWords
you are putting an illegal method declaration inside the method. You need to end the promptForWords
method before starting your declaration of the promptForStoryTemplate
method.
The other errors are related to a single ending curly brace. You can match curly braces. Take your time.
coder5837
1,535 Pointspackage 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 e) {
System.out.println("There was a problem prompting for words");
e.printStackTrace();
System.exit(0);
}
String story = tmpl.render(results);
System.out.printf("%nYour TreeStory:%n%n%s", story);
}
/**
* 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 BufferedReader getmReader() {
return mReader;
}
public String promptForWord(String phrase) {
String word = null;
while (mCensoredWords.contains(word) || word == null) {
try {
System.out.printf("Please enter a %s: ", phrase);
word = mReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (mCensoredWords.contains(word)) {
System.out.print("Oops! That word is censored. Please enter another word: ");
}
}
return word;
}
public String promptForStoryTemplate() {
System.out.printf("Enter the story: ");
String line = null;
try {
line = mReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
}
}
/**
* 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 BufferedReader getmReader() {
return mReader;
}
public String promptForWord(String phrase) {
String word = null;
while (mCensoredWords.contains(word) || word == null) {
try {
System.out.printf("Please enter a %s: ", phrase);
word = mReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (mCensoredWords.contains(word)) {
System.out.print("Oops! That word is censored. Please enter another word: ");
}
}
return word;
}
public String promptForStoryTemplate() {
System.out.printf("Enter the story: ");
String line = null;
try {
line = mReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
}
./com/teamtreehouse/Prompter.java:114: error: class, interface, or enum expected
}
^
./com/teamtreehouse/Prompter.java:122: error: class, interface, or enum expected
public BufferedReader getmReader() {
^
./com/teamtreehouse/Prompter.java:124: error: class, interface, or enum expected
}
^
./com/teamtreehouse/Prompter.java:126: error: class, interface, or enum expected
public String promptForWord(String phrase) {
^
./com/teamtreehouse/Prompter.java:129: error: class, interface, or enum expected
while (mCensoredWords.contains(word) || word == null) {
^
./com/teamtreehouse/Prompter.java:132: error: class, interface, or enum expected
word = mReader.readLine();
^
./com/teamtreehouse/Prompter.java:133: error: class, interface, or enum expected
} catch (IOException e) {
^
./com/teamtreehouse/Prompter.java:135: error: class, interface, or enum expected
}
^
./com/teamtreehouse/Prompter.java:139: error: class, interface, or enum expected
}
^
./com/teamtreehouse/Prompter.java:143: error: class, interface, or enum expected
}
^
./com/teamtreehouse/Prompter.java:145: error: class, interface, or enum expected
public String promptForStoryTemplate() {
^
./com/teamtreehouse/Prompter.java:147: error: class, interface, or enum expected
String line = null;
^
./com/teamtreehouse/Prompter.java:148: error: class, interface, or enum expected
try {
^
./com/teamtreehouse/Prompter.java:150: error: class, interface, or enum expected
} catch (IOException e) {
^
./com/teamtreehouse/Prompter.java:152: error: class, interface, or enum expected
}
^
./com/teamtreehouse/Prompter.java:154: error: class, interface, or enum expected
}
^
16 errors
alright so this is my code right now and what I'm deciphering from the errors is that public is not a valid way to start the line, and while isn't either. I checked all of my parsing and there doesn't seem to be a problem with that either though. It also says that at 114 I still need to put a } there but I already have!
Ben Jakuben
Treehouse TeacherEmily, you are SO close! Kudos, Craig Dennis, for working so hard at explaining step by step, and kudos to you, Emily, for sticking with a frustrating problem for so long.
If you read carefully through the last code you posted, you'll notice that you have a certain section in there twice. That's problematic by itself, but the first time it appears it has the extra curly brace that is the cause of all your trouble. I want you to delete EXACTLY this block of code from the center (I don't have your exact line numbers):
/**
* 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 BufferedReader getmReader() {
return mReader;
}
public String promptForWord(String phrase) {
String word = null;
while (mCensoredWords.contains(word) || word == null) {
try {
System.out.printf("Please enter a %s: ", phrase);
word = mReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (mCensoredWords.contains(word)) {
System.out.print("Oops! That word is censored. Please enter another word: ");
}
}
return word;
}
public String promptForStoryTemplate() {
System.out.printf("Enter the story: ");
String line = null;
try {
line = mReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
}
}
Read each name of the three methods there and notice that they are duplicated below. The duplicate section has the correct curly braces, though.
coder5837
1,535 PointsTHANK YOU SO MUCH Craig Dennis Ben Jakuben ICANNOT TAHNK YOU ENOUGH I LITERALLY JUMPED FOR JOY FOR FIVE MINUTES AFTER I FINALLY PASSED IT THANK YOU FOR STICKING WITH ME EVEN THOUGH IT WAS PROBABLY IRRITATING FOR HOURS OMG OMGOMGOMGMGMGOMGMOG I FEEL LIKE I JUST CONQUERED EARTH I AM SO SSOSOSOOSOSOSOSOSOSOOSOSOSOSOSOSOSOSOSOSOSOSO HAPPY THANK YOU THANK YOU THANK YOU
Seth Kroger
56,413 PointsYou forgot to take care of the TODO listed as #1 and #1.a in the "what I am testing".
coder5837
1,535 Pointshow do I do that?
coder5837
1,535 Pointsif you could post the completed code that would be great I am so done with this course and I would just like to be finished with it
coder5837
1,535 Pointscoder5837
1,535 Pointsagain, please make sure to just have the answer and an explanation I am completely done with "hints"!