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

Ricky Chandra
Ricky Chandra
1,239 Points

Explanation for my compile error on challenge.

I've done all of the TODO's and it can be compiled in my local environment. But, the system said that my code is compiled error. Can you help me to explain please?

Challenge Source Code : [https://teamtreehouse.com/library/teamwork]

Main.java

package com.teamtreehouse;

import java.io.IOException;

public class Main {

    public static void main(String[] args) throws IOException {
        Prompter prmpt = new Prompter();
        String story = prmpt.promptForStory();
        Template tmpl = new Template(story);
        String results = prmpt.run(tmpl);
        System.out.printf("Your TreeStory:%n%n%s", results);
    }
}

Prompter.java

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 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);
        }
        return tmpl.render(results);
    }

    public String promptForStory() throws IOException {
        String story = "";
        try {
            System.out.println("Enter a story template : ");
            story = mReader.readLine();
        }
        catch (IOException ioe){
            System.out.println("Something error in your input!");
            ioe.printStackTrace();
        }
        return 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 String promptForWord(String phrase) throws IOException {
        String responseWord;
        boolean goodResponse;
        do {
            System.out.printf("Enter %s : ", phrase);
            responseWord = mReader.readLine();
            goodResponse = !isCensoredWord(responseWord);
        }
        while (!goodResponse);
        return responseWord;
    }

    public boolean isCensoredWord(String word){
        boolean isCensoredWord;
        if (mCensoredWords.contains(word)){
            isCensoredWord = true;
            System.out.printf("%s is not allowed. Please try another word%n", word);
        }
        else {
            isCensoredWord = false;
        }
        return isCensoredWord;
    }
}

1 Answer

I copied your code into my ide and it worked. Are you forgetting the Template class? If so here is mine.

public class Template {
    /**
     * The string format that is waiting to receive values
     */
    private String mCompiled;
    private List<String> mPlaceholders;

    /**
     * @param text A template with double underscored surrounded placeholders. eg: Hello __name__!
     */
    public Template(String text) {
        // Match on double underscore surrounded words, like __name__ or __proper noun__
        Pattern pattern = Pattern.compile("__([^__]+)__");
        Matcher matcher = pattern.matcher(text);
        mPlaceholders = new ArrayList<String>();
        while (matcher.find()) {
            String label = matcher.group(1);
            mPlaceholders.add(label);
        }
        mCompiled = matcher.replaceAll("%s");
    }


    /**
     * @return Ordered names of placeholders in the template
     */
    public List<String> getPlaceHolders() {
        return mPlaceholders;
    }


    /**
     * Given a list of values, replaces the fill in the blanks in order.
     *
     * @param values The replacements for the fill in the blank
     * @return The filled out TreeStory
     */
    public String render(List<String> values) {
        // String.format accepts the templates and Object... (a variable amount of objects)
        return String.format(mCompiled, values.toArray());
    }
}