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

Matt Malone
Matt Malone
11,437 Points

New question thread to Steve Hunter @stevehunter about the "promptForWord" method in Prompter.java

In lines 87-91 of Prompter.java, I've left some commented code in around "return word;". What I'd wanted to do in the 'promptForWord' method in Prompter.java was to:

  1. return the word if it is not among the 'mCensoredWords'.
    or
  2. To somehow enable the 'promptForWords' (plural) method to not iterate to the next listed placeholder if the word is among the 'mCensoredWords'.

My problem is that in 'promptForWord', I am required by Java to return something. That something will force the 'promptForWords' to iterate to the next placeholder, thus not allowing the user to have a chance to enter a non-censored word into that placeholder.

I'm not sure in which method I should "stop the interating" so that the user has a chance (after entering in a censored word) to enter in a non-censored word.

It seems to work okay if I do not enter a censored word.

Thanks!

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

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();
        String story = prompter.promptForStoryTemlate();

        Template tmpl = new Template(story);
        prompter.run(tmpl);
    }
}
com/teamtreehouse/Prompter.java
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;
import java.util.Scanner;

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

    public Prompter() {
        mReader = new BufferedReader(new InputStreamReader(System.in));
        loadCensoredWords();
    }

    public String promptForStoryTemlate() {
        Scanner reader = new Scanner(System.in);  // Reading from System.in
        System.out.println("Enter a story template: ");
        String sentence = "";
        if (reader.hasNext()) {
            sentence = reader.nextLine();
        }
        return sentence;
    }

    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 String 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);
        }
        // TODO:csd - Print out the results that were gathered here by rendering the template
        System.out.printf("Your TreeStory:%n%n%s", tmpl.render(results));
        return null;
    }

    /**
     * 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) {
        // TODO:csd - Prompt the user for the response to the phrase, make sure the word is censored, loop until you get a good response.
        Scanner reader = new Scanner(System.in);  // Reading from System.in
        System.out.printf("Enter a %s: ", phrase);
        String word = reader.next();

//        if (mCensoredWords.contains(word)) {
//            ;
//        } else {
            return word;
//        }

    }
}
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

I'll have a look tomorrow. Nearly midnight here and I need sleep!

Matt Malone
Matt Malone
11,437 Points

Okay thanks! I'll take a look at this.

2 Answers

Hi Andrew,

You could use a while loop to say:

do{
  // ask for word
  word = // get user input;
} while(mCensoredWords.contains(word));

That way, the loop will continue and the user will be re-prompted to enter a word if they enter a censored one. If they enter a 'valid' word, the loop stops and you can process the entered word.

I think you're better processing the invalid entry in the method that is prompting for the entry; that's that method's job - to secure valid user input. That is it's single responsibility. The other method's responsibility is to make up the sentence using validated words.

Make sense?

Steve.

Matt Malone
Matt Malone
11,437 Points

Thanks so much, I was able to solve it! I also realized that I'd needed only one Scanner object -- not two, like I'd written before -- for the story template input and for the prompt for word.

:+1: :+1: :+1: :+1: Good work! :smile: