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

Balázs Szente
Balázs Szente
11,377 Points

When checking String == null I get - Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

On line 79 when I check newWord == null I got Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Arrays.java:3236) at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118) at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93) at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153) at java.io.PrintStream.write(PrintStream.java:480) at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221) at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291) at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:104) at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:185) at java.io.PrintStream.write(PrintStream.java:527) at java.io.PrintStream.print(PrintStream.java:669) at java.io.PrintStream.append(PrintStream.java:1065) at java.io.PrintStream.append(PrintStream.java:57) at java.util.Formatter$FormatSpecifier.print(Formatter.java:2913) at java.util.Formatter$FormatSpecifier.printString(Formatter.java:2886) at java.util.Formatter$FormatSpecifier.print(Formatter.java:2763) at java.util.Formatter.format(Formatter.java:2520) at java.io.PrintStream.format(PrintStream.java:970) at java.io.PrintStream.printf(PrintStream.java:871) at com.teamtreehouse.Prompter.promptForWord(Prompter.java:76) at com.teamtreehouse.Prompter.promptForWords(Prompter.java:59) at com.teamtreehouse.Prompter.run(Prompter.java:38) at com.teamtreehouse.Main.main(Main.java:25) at JavaTester.run(JavaTester.java:77) at JavaTester.main(JavaTester.java:39)

And when I don't check for null and test with this loop: while (mCensoredWords.contains(newWord));

I recieve the following message: Bummer! Looks like you didn't present the TreeStory (3)

Could you tell me what did I wrong?

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Arrays;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        // write your code here
        //String story = "Make me a __placeholder1__ and I'll __placeholder2__ it __placeholder3__ __placeholder4__.";
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String story = null;
        try {
            story = reader.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Template tmpl = new Template(story);

        Prompter pr = new Prompter();
        pr.run(tmpl);

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

    /**
     * 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 newWord = "";
        do {
            //System.out.println("Sorry, this word is censored, try again.");
            System.out.printf("%nWrite me one %s: ", phrase);
            newWord = mReader.readLine();
        } while (mCensoredWords.contains(newWord) ||
                 newWord == null);

        return newWord;
    }
}
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

2 Answers

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

Hi, I played around. It seems that you have to refactor you code, so that:

  1. Buffered reader is used only in Prompter
  2. Implement Prompter.promptForStory() method that returns story

Main class should look like this *without imports":

public class Main {

    public static void main(String[] args) {
        // write your code here
        //String story = "Make me a __placeholder1__ and I'll __placeholder2__ it __placeholder3__ __placeholder4__.";
        Prompter pr = new Prompter();

        String story = pr.promptForStory();

        Template tmpl = new Template(story);

        pr.run(tmpl);

    }
}

And in Prompter add:

    // new method introduced by me
    public String promptForStory() {
        String story = "";
        try {
           story = mReader.readLine();
        } catch (IOException ioe) {
            System.out.println("Error reading user input");
            System.exit(1);
        }
        return story;
    }

The rest you can leave as is.

Try that out and let me know. You see the problem is the way tests work. Craig wanted and assumed that BufferedReader will be inside prompter and you WILL prompt for story there. I mean that is the reason why we called the class Prompter right? :)

Oh yeah, and also Craig assumed that you will create Prompter BEFORE prompting for Story, that is the reason why I assume you got those NULL errors...I guess

Anyway hope It'll work. Nice code BTW, awesome Markdown formatted, was really easy to copy, thanks for that.

Balázs Szente
Balázs Szente
11,377 Points

Thank you for your answer, this is the right solution what i was looking for. You saved me a lot of time with that.

Sometimes I misunderstood the requirements and try to solve the problems in the wrong way.

I thought all I need is just to complete the 3rd task, but as you pointed out, the other two tasks weren't completed fully yet.