Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Local Development Environments Advanced Tooling Teamwork

James Croxford
James Croxford
12,697 Points

IDE Course final exercise- extending past the TODOs requirements

So I've been playing round with this exercise, and I've intended to extend a few parts regarding input validation and normalization that seem to work inside Intellij, but not within workspaces.

Firstly, the exercise is this one: "Thanks again for wrapping this up for me. I've written some tests that make sure all the TODO's are completed. I wrote some pseudo-code tests, so you understand what I am looking for. When you are done with each TODO, remove it from the code.

When all TODO's are complete, manually verify that the pseudo-tests pass and then copy and paste your code into the appropriate files."

First up is my main.java

package com.teamtreehouse;

public class Main {

    public static void main(String[] args) {
        Prompter storyPrompter = new Prompter();
        String text = storyPrompter.promptForStory();
        Template tmpl = new Template(text);
        storyPrompter.run(tmpl);
    }
}

And then my 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.*;


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 finalResult = tmpl.render(results);
        System.out.printf("Your TreeStory:%n%n%s", finalResult);
    }

    public String promptForStory() {
        String story = null;
        System.out.println("Time to write a story- it should contain descriptors, surrounded by double underscores, e.g.: __name__ , __verb__ for you to fill in!");
        System.out.println("For example:'__name__ was a big collector of __noun__. Their favourite __noun__ was a __adjective_ __noun__'");
        System.out.println("Enter your story below:  ");
        try {
            story = mReader.readLine();
        }   catch (IOException e) {
            e.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 word = null;
        System.out.printf("Enter a(n) %s: %n", phrase);
        boolean goodResponse = false;
        do {
            word = mReader.readLine().trim().toLowerCase();
            if (!mCensoredWords.contains(word)) {
                goodResponse = true;
            }
        } while (!goodResponse);
        return word;
            }
            //why does including .trim and .tlc not work in workspaces?

    }

As can be seen, I've added in

word = mReader.readLine().trim().toLowerCase();

Which appears to work inside Intellij, but I ended up deleting to pass the exercise in workspaces. Is this something to do with how the checks for the exercise were written by Craig/a workspaces bug? Or is there a genuine code issue here?

And for reference, Template.java

package com.teamtreehouse;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


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

LASTLY: I also aimed to extend to include cases in which the user simply inputs the enter key, or forces in non numerical and alphabetical keys. I intended to start with something along the lines of:

   response = mReader.readLine().trim().toLowerCase();
        for (char letter: response.toCharArray())
        {
            if (! Character.isLetter(letter) || ! Character.isDigit(letter)) {
                System.out.printf("A letter or number are required!");
                System.out.printf("Enter your %s%n", phrase);
                response = mReader.readLine().trim().toLowerCase();       
            }
        }
        if (response.length() == 0) {
            System.out.printf("No letters or numbers found, please try again! %n");
            System.out.printf("Enter your %s%n", phrase);
            response = mReader.readLine().trim().toLowerCase();
        }

Before moving into the censored words part. However I couldn't quite see how to fit this all into a loop that checks for a good response without making things overly complicated. Would I be better moving these validation aspects to another seperate method? If not can anyone give me any hints into how best to incorporate these additional features?

Thanks!