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

Finishing TreeStory challenge not working

I run the code locally and it works

Connected to the target VM, address: '127.0.0.1:52634', transport: 'socket'

Out put ++++++++++++++++++++++++++++++++++++++++++++++++ Story Templat

Thanks name for helping me out. You are really a adjective noun and I owe you a noun.

Enter name: Mark Enter adjective: Great Enter noun: Eater Enter noun: Hotdog Disconnected from the target VM, address: '127.0.0.1:52634', transport: 'socket' [Mark, Great, Eater, Hotdog] Your TreeStory:

Thanks Mark for helping me out. You are really a Great Eater and I owe you a Hotdog Process finished with exit code 0 ++++++++++++++++++++++++++++++++++++++++++++++++++++

but the Recheck Work keep running into Exception. This is the Exception from recheck

Story Templat

Thanks name for helping me out. You are really a adjective noun and I owe you a noun.

Enter name: Enter adjective: java.util.NoSuchElementException: No line found at java.util.Scanner.nextLine(Scanner.java:1540) at com.teamtreehouse.Prompter.promptForWord(Prompter.java:71) at com.teamtreehouse.Prompter.promptForWords(Prompter.java:55) at com.teamtreehouse.Prompter.Startrun(Prompter.java:35) at com.teamtreehouse.Main.main(Main.java:31) at JavaTester.run(JavaTester.java:77) at JavaTester.main(JavaTester.java:39)

=========================================================================

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

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

public class Main {

    private static String mCompiled;
    private static List<String> mPlaceholders;
    private static List<String> results;
    public static void main(String[] args) {
        // write your code here

        System.out.printf("Story Templat%n%n Thanks __name__ for helping me out.  You are really a __adjective__ __noun__ and I owe you a __noun__.%n%n");
        Scanner inputReader = new Scanner(System.in);
        String story = null;
        if(story == null || story.isEmpty()){
            story = "Thanks __name__ for helping me out.  You are really a __adjective__ __noun__ and I owe you a __noun__";
        }
        //String story = "Thanks __name__ for helping me out.  You are really a __adjective__ __noun__ and I owe you a __noun__.";
        template(story);


        List<String> fakeResults = Arrays.asList("friend", "talented", "java programmer", "high five");

        Prompter prompter = new Prompter();
        results = prompter.Startrun(mPlaceholders);
        System.out.println(results);
        String TreeStroy = render(results);
        //String results = render(fakeResults);
        System.out.printf("Your TreeStory:%n%n%s", TreeStroy);

    }


    public static void 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<>();
        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 static String render(List<String> values) {
        // String.format accepts the templates and Object... (a variable amount of objects)
        return String.format(mCompiled, values.toArray());
    }

}
com/teamtreehouse/Prompter.java
package com.teamtreehouse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
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>();
        List<String> words =new ArrayList<>() ;
        try {
            words.add("Stupid");
            words.add("dork");
            words.add("ugly");
            mCensoredWords.addAll(words);
        }catch(Exception ioe){
            System.out.printf("Exception %s", ioe.getStackTrace());
        }

    }

    public List<String> Startrun(List<String> 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 results;

    }

    /**
     * Prompts user for each of the blanks
     *
     * @param tmpl The compiled template
     * @return
     * @throws IOException
     */
    public List<String> promptForWords(List<String> tmpl) throws IOException {
        List<String> words = new ArrayList<String>();
        for (String phrase : tmpl) {
            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 {
        System.out.printf("Enter %s: ", phrase);
        Scanner inputReader = new Scanner(System.in);
        String userInput = inputReader.nextLine();

        return userInput;
    }
}
pseudo-tests.md

4 Answers

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

Hi.

Your answer is correct and your TreeStory works, but the problem is in order to pass the challenge you have to have certain structure.

Namely Main method should look like this:

public class Main {

    public static void main(String[] args) {

         Prompter pr = new Prompter();

        //prompt for 
        // String story = "Make me a __placeholder1__ and I'll __placeholder2__ it __placeholder3__ __placeholder4__.";
        String story = pr.promptForStory();

        Template tmpl = new Template(story);

        pr.run(tmpl);

    }
}

There should be no BufferedReader or Scanner in this class, and all prompting MUST be in Prompter

Second. Please do not use Scanner.

Craig tests are mocking BufferedReader when he is testing your code. (For more about mocking, please google or take Unit Testing and 'Unit Testing with Spring' Courses, esp. the last one)

Which means you cannot use Scanner, sorry, so implement all prompting methods with BufferedReader and only in Prompter class.

Please write Prompter.promptForStory method yourself, it prompts user for story once and returns user input as a String

Also in Prompter.promptForWord you have to censor bad words, i.e. don't allow them to be accepted if they are censored.

That should enough for a start, ask if you have more questions...

And one more. DO NOT change Craigs methods. All of them has to be there. The only new method that you have to introduce is Prompter.promptForStory. The rest of the methods should be left same

Hi

The problem is that the chllenge does not include Template.java in the set up. There is only Main.java and Prompter.java. Therefore, I can't do what you are suggesting:

Template tmpl = new Template(story);

Since there is no Template.java in the challenge.

Any idea how to work aroud this missing Template.java. BTW, this is the reason why I made changes to Craigs' code.

Thanks

Hi

I know they are available for download. Please let me clarify

I think the confusion is from the fact that if you start the challenge, you will be presented with 2 java files and an md file. The 2 java files are Main.java and Promptor.java. There is no Template.java provided. I know there is one in the downloadable project. But in the challenge, it is not included

So in general

Template tmpl = new Template(story);

should not work .... since there is no Template.java provided in the challenge.

Based on this conversation, I suspect, Template.java existing in the background ... e.g.; we need to assume that it exist but not visible to us? If that not true, please explain how do we add a file Template.java to the challenge. I know I can download it ... The question is where/how does it fit in the Challenge ... is it in the background?

Thanks

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

this file is in "background" and it does look exactly like Template in project download files... Craig specifically removed it, so that you don't change it...

Thanks for the reply ... I think I can handle it from there once I get back home. But for Craigs, I think the Challenge should specifically not that this file is in the background, and that is different from other challenges that I have done. Time is important, and I think I wasted a lot of time doing a working work around