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

Gena Aivazian
Gena Aivazian
16,574 Points

Getting Bummer errors.. I dont understand why it is failling. I have tried a lot of things but no success..

Hi, please take a look and tell me why it is failing. In IntelliJ it is working perfectly but here it keeps failing with different errors.

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

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        // write your code here
        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);
        try {

            List<String> res = prompter.promptForWords(tmpl);
            String results = tmpl.render(res);
            System.out.printf("Your TreeStory:%n%n%s", results);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
com/teamtreehouse/Prompter.java
package com.teamtreehouse;
import java.io.Console;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;


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");
        System.out.println(file);
        List<String> words = null;
        try {
            words = Files.readAllLines(file);
        } catch (IOException e) {
            System.out.println("Couldn't load censored words");
            e.printStackTrace();
        }
        assert words != null;
        mCensoredWords.addAll(words);
    }

    public void run(Template tmpl) {
        List<String> results = null;
        try {
            results = promptForWords(tmpl);
            tmpl.render(results);

        } 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) throws IOException {
        boolean isValid = false;
        String userInput = "";
        while (!isValid) {
            System.out.printf("Enter your answer to the phrase '%s': ", phrase);
            try {
                userInput = this.mReader.readLine();
            } catch (IOException e) {
                System.out.println("Error reading input: " + e.getMessage());
                throw e; // rethrow the exception to be handled by the caller
            }

            if (userInput == null) {
                throw new IOException("Null input received from user.");
            }

            isValid = true;

            for (String censoredWord : mCensoredWords) {
                if (userInput.equalsIgnoreCase(censoredWord)) {
                    isValid = false;
                    System.out.println("That word is not allowed");
                    break;
                }
            }
        }
        return userInput;
    }
}
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

1 Answer

Rohald van Merode
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Rohald van Merode
Treehouse Staff

Hey Gena Aivazian 👋

You're pretty close to getting this working! The issue here seems to be the difference between the tests you're doing in IntelliJ and the ones setup for the challenge. In the pseudo-tests.md file you can see the requirements needed to pass the tests in the challenge. Looking at the first one of them the challenge requires you to set up a method that prompts the user for a story template. In your Main.java however you're hardcoding a story variable instead of prompting the user for it.

You'll instead want to create a new method in the Prompter that prompts the user for a story template. Then you call that new method and store it's returned value in the story variable instead of the string it's currently holding 🙂

I hope this helps to get you going again! If not let me know and I'd be more than happy to elaborate! 🙂

Gena Aivazian
Gena Aivazian
16,574 Points

Thank you so much. That helped. I finally solved it.