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

Solomon Alexandru
Solomon Alexandru
3,633 Points

Could someone please explain me what I'm doing wrong?

I'm trying to get past this exercise, but I'm not really understanding the TODO's, so probably I messed up by doing someone different from what was asked. Could someone please help me?

This is the main class:

package com.teamtreehouse;

import java.util.Arrays;
import java.util.List;

public class Main {

    public static void main(String[] args) {
    // write your code here
        // TODO:csd - Instantiate a new Prompter object and prompt for the story template

        Prompter prompter = new Prompter();
        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);
        // TODO:csd - Use the prompter object to have it do the prompting, censoring and outputting.  Call Prompter.run
        prompter.run(tmpl);
        List<String> fakeResults = Arrays.asList(
                "friend",
                "talented",
                "java programmer",
                "high five");
        // TODO:csd - This should really happen in the Prompter.run method, let's get these implemetation details out of the main method


    }
}

Also, the prompter class

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();
    }

    private void loadCensoredWords() {
        mCensoredWords = new HashSet<String>();
        Path file = Paths.get("TreeStory2", "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);
        }
        // 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));
    }

    /**
     * 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 user_input = new Scanner(System.in);
        System.out.print("Please respond to the phrase:");
        String responseToPrase = user_input.next();
        while (mCensoredWords.contains(responseToPrase))
        {
            System.out.print("That word is censored! Please write another word:");
            responseToPrase = user_input.next();
        }
        return "";
    }
}

5 Answers

Brendon Butler
Brendon Butler
4,254 Points

Looks like all you have to do is remove the "//TODO" lines from the code before you run it.

Solomon Alexandru
Solomon Alexandru
3,633 Points

I did that but it gives me "java.lang nullpointerexception"

Brendon Butler
Brendon Butler
4,254 Points

Could you post the full stacktrace/error from the "preview" window?

Solomon Alexandru
Solomon Alexandru
3,633 Points

In my local machine it works, but here on the online development environment it's not. It gives me the following error:

Please respond to the phrase:Please respond to the phrase:java.util.NoSuchElementException at java.util.Scanner.throwFor(Scanner.java:862) at java.util.Scanner.next(Scanner.java:1371) at com.teamtreehouse.Prompter.promptForWord(Prompter.java:74) at com.teamtreehouse.Prompter.promptForWords(Prompter.java:58) at com.teamtreehouse.Prompter.run(Prompter.java:39) at com.teamtreehouse.Main.main(Main.java:13) at JavaTester.run(JavaTester.java:77) at JavaTester.main(JavaTester.java:39)

"

Brendon Butler
Brendon Butler
4,254 Points

"TreeStory2/resources/censored_words.txt"

Looks like this path or file doesn't exist.

Solomon Alexandru
Solomon Alexandru
3,633 Points

I saw that error afterwards, after editing it properly it gave me the following error I added at the edited comment above

Brendon Butler
Brendon Butler
4,254 Points

So far I'm stumped. Try using "user_input.nextLine()" instead of "user_input.next()" then get back to me on whether or not it worked. Looks like there's a few instances of you using ".next()" instead of ".nextLine()"

Solomon Alexandru
Solomon Alexandru
3,633 Points

Did that, got the following: Please respond to the phrase:Please respond to the phrase:java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1540) at com.teamtreehouse.Prompter.promptForWord(Prompter.java:74) at com.teamtreehouse.Prompter.promptForWords(Prompter.java:58) at com.teamtreehouse.Prompter.run(Prompter.java:39) at com.teamtreehouse.Main.main(Main.java:14) at JavaTester.run(JavaTester.java:77) at JavaTester.main(JavaTester.java:39)

You can check out the method promptForWord written below.

public String promptForWord(String phrase) throws IOException {
    String inputtedWord;
    System.out.printf("Please input your word for %s : ", phrase);
    inputtedWord = mReader.readLine().toLowerCase().trim();
    while (inputtedWord.length() == 0 || mCensoredWords.contains(inputtedWord) ) {
        System.out.printf("%nYou didn't input any word or it's censored, please input %s again: ", phrase);
        inputtedWord = mReader.readLine().toLowerCase().trim();
    }
    return inputtedWord;
}

And don't forget to add a new method in Prompter.java you'll have to have a new public method to get your story template as described in the pseudo-tests.md