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 Finishing TreeStory

Sachinrao Panemangalore
Sachinrao Panemangalore
1,658 Points

Out of Heap memory in browser

The code works fine locally. However when I submit it into the browser I seem to be running out of heap memory . Please find attached my code .

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

import java.util.Arrays;
import java.util.List;

public class Main {

    public static void main(String[] args) {
        //Instantiate a new Prompter object and prompt for the story template
        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);
        //Use the prompter object to have it do the prompting, censoring and outputting.  Call Prompter.run
        prompter.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);
        }
        // Print out the results that were gathered here by rendering the template
        // List<String> fakeResults = Arrays.asList("friend", "talented", "java programmer", "high five");
        String renderedResults = tmpl.render(results);
        System.out.printf("Your TreeStory:%n%n%s", renderedResults);
    }

    /**
     * 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 response;

        //Prompt the user for the response to the phrase, make sure the word is censored, loop until you get a good response.
        boolean clean = false;
        do {
            System.out.printf("%s\n",phrase);
            response = mReader.readLine();
            if( response != null ) {
                response = response.replaceAll("\\s+","");
                //System.out.printf("Response length = %d\n",response.length());
                //check if response contains any bad words.
                if (response.length() > 0) {
                    clean = true;
                    for (String word : mCensoredWords) {
                        if (response.toLowerCase().contains(word)) {
                            System.out.printf("\n Sorry '%s' is a censored word...\n...please retry..", word);
                            clean = false;
                            break;
                        }
                    }
                }
            }
        } while(!clean);

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

3 Answers

Usually "Heap memory" errors means you are in an infinite loop. I am assuming the clean variable in "while(!clean);" is never changing to "true" meaning

                if (response.length() > 0) {
                    clean = true; 
                    // Never gets executed. Probably due to the wrong regular expression used in replaceAll().

or

                        if (response.toLowerCase().contains(word)) {
                            System.out.printf("\n Sorry '%s' is a censored word...\n...please retry..", word);
                            clean = false; // Always sets clean to false, causing while(!clean) to infinite loop.
                            break;
                        }

This is a perfect opportunity for you to try out the IDE's debugger and step through the code and see what is happening exactly.

Some advice would be too clean up the promptForWord() method since it looks needlessly complicated. A lot of the logic is a not needed at all or repeat of code.

Sachinrao Panemangalore
Sachinrao Panemangalore
1,658 Points

Hi inversesquarelaw I indeed had tested with debugger locally for my test cases. The code works perfectly fine for

  1. success cases
  2. error cases ( with censored words).
  3. blanks ( user just hits enter ).

I think there is a problem with the test code running in the web backend. I often get ( communication error ) . It doesnt seem to process printf correctly.

Based on your feedback I refactored the code as follows:

public String promptForWord(String phrase) throws IOException { String response = null; //Prompt the user for the response to the phrase, make sure the word is censored, loop until you get a good response. while((response == null) || mCensoredWords.contains(response)){ System.out.printf("%s\n", phrase); response = mReader.readLine(); //note: this check was added after I got NullPointer Exception in Browser ( not required locally). if( response != null ){ //get rid of whitespaces. trim() too works. response.replaceAll("\s+",""); if ( response.length() > 0 ) { response = response.toLowerCase(); } else { response = null; } } } return response; }

Harry James
Harry James
14,780 Points

Hey Sachinrao,

As inversesquarelaw has pointed out, this is usually caused by an infinite loop.

From looking at the code, it seems as though if you were to pass in a word that does not contain a censored word, the loop will run indefinitely, and cause the Out of Heap error.

Go through your do/while loop, as if the phrase you have entered does not contain a censored word, you should find that clean never becomes false, and that the loop will run forever.


Hope it helps and if you have any more problems, feel free to give me a shout :)

The problem is most likely due to your code because you code is taking too long to run and causing the connection problem. There is probably a test case that you did not account for in your local test that Treehouse is testing for.

Another less likely possibility is that you have a bad internet connection. You can try to paste other people's working code as a solution and see if you get the same connection problem error. If you don't then it probably isn't a bad internet connection.

You code is hard to read since it wasn't formatted with markdown (used 3 backticks, next to the "1" key). And I don't really want to go through and debug your code.

Sachinrao Panemangalore
Sachinrao Panemangalore
1,658 Points

Thanks a lot Harry James & Inversesquarelaw for the help and insight. I was able to fix the problem . I am adding the details below, as I think it might help others if they face the same issue.

  1. The Problem: I was not prompting the user for the Story template. I had considered the TBDs to be the source of truth and ignored the pseudo-test md file.
  2. How did I know this: I changed the code to throw an Exception on the first input and printed the input values.

Thanks again!