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

Christopher Earle
PLUS
Christopher Earle
Courses Plus Student 22,340 Points

Error: Bummer! Your code took too long to run.

Hi, I'm having some problems passing the Finishing TreeStory Challenge,. I keep receiving the error listed in the title. I've run the code in IntelliJ using the pseudo-tests as a guide, and it seemed to meet the criteria. Do you have any advice or helpful hints to correct my code?

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

    public static void main(String[] args) {

        Prompter prompter = new Prompter();
        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Please enter the story template:  ");
        String story = null;
        try {
            story = buffer.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Template tmpl = new Template(story);

        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.*;


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> resultsString = tmpl.getPlaceHolders();
        List<String> result = null;
        try {
            result = promptForWords(tmpl);
        } catch (IOException e) {
            System.out.println("There was a problem prompting for words");
            e.printStackTrace();
            System.exit(0);
        }
        String results = tmpl.render(result);
        System.out.printf("Your TreeStory:%n%n%s", results);
    }

    /**
     * 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) {

        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Please enter a word:  ");
        String word = null;
        while(mCensoredWords.contains(word) || word == null) {
            try {
                word = buffer.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(mCensoredWords.contains(word)){
                System.out.println("Oops! That word is censored. Please enter another word");
            }
        }
            return word;
        }
    }
pseudo-tests.md
#  This is essentially what I am testing 
1.  The user is prompted for a string template (the one with the double underscores in it)

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

Hi Christopher, Few that's a lot of code! Are you trying to do the same as Mr. Craig's lecture or are you experimenting a little bit? I also wondered when you said "I've run the code in IntelliJ using the pseudo-tests as a guide." Is IntelliJ what you were using instead of workspaces? I may be able to help if you could answer these questions, particularly the one about IntelliJ.

Ken Alger
Ken Alger
Treehouse Teacher

Peter Price -

For this challenge and course IntelliJ is the development tool used.

Christopher Earle -

I had similar issues running my code successfully in IntelliJ and then having the code challenge not like my code. I was getting different errors than your code is producing in the challenge checker though. The error you are getting typically is the result of an endless loop or similar issue that results in a never ending issue. Can you find it???

Ken

Christopher Earle
Christopher Earle
Courses Plus Student 22,340 Points

Hi Ken,

Thanks for the advice! I tried to cleanup the promptForWord method to avoid any endless loop scenarios, but I'm still stuck on this challenge. The error message now reads "Bummer! Try again!," and the compiler outputs "Exception in thread "main" java.lang.OutOfMemoryError: Java heap space." I've pasted the updated promptForWord method below. Any thoughts?

public String promptForWord(String phrase) {
    BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
    String word = null;
    while(word == null || mCensoredWords.contains(word)) {
        System.out.println("Please enter a word:  ");
        try {
            word = buffer.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return word;
}

4 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Remember:

2.  The user is then prompted for each word that has been double underscored.

What if you made your prompt include the phrase?

Sidenote: mReader exists for your use already.

Christopher Earle
PLUS
Christopher Earle
Courses Plus Student 22,340 Points

Hi Craig,

Thanks for the hints! I included the phrase in the promptForWord prompt, but I wasn't able to pass the challenge until I migrated the story prompt from main to prompter to utilize the existing BufferedReader. Doh!

Andrej Fokin
Andrej Fokin
2,057 Points

I have the same error, can you please tell me in detail what did you did ?

Christopher Earle
PLUS
Christopher Earle
Courses Plus Student 22,340 Points

Hi Andrej,

To correct the issue, I did the following: 1) I made my prompt for word include the phrase variable so that the user knew the word type (noun, verb, etc.) 2) I moved all prompts from main to prompter. 3) I reused the mReader BufferedReader in the promptForWord method.

You may have a different code issue though (See Ken's post). If you're still having trouble, post your code to the forum for help.

Andre Colares
Andre Colares
5,437 Points

Can u post your code. I'm getting this error: 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:113) at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93) at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:140) 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.println(PrintStream.java:806) at com.teamtreehouse.Prompter.promptForWord(Prompter.java:66) at com.teamtreehouse.Prompter.promptForWords(Prompter.java:56) at com.teamtreehouse.Prompter.run(Prompter.java:42) at com.teamtreehouse.Main.main(Main.java:16) at JavaTester.run(JavaTester.java:77) at JavaTester.main(JavaTester.java:39)

I agree with Andre Colares, I am absolutely stumped. This is the last thing keeping me from finishing this course.

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hey John Weland ! Does your code exist somewhere I can take a peek at? This one is a little tricky.

package com.teamtreehouse;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {

    public static void main(String[] args) {

        Prompter prompter = new Prompter();
        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Please enter the story template:  ");
        String story = null;
        try {
            story = buffer.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Template tmpl = new Template(story);

        prompter.run(tmpl);
    }
}
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> resultsString = tmpl.getPlaceHolders();
        List<String> result = null;
        try {
            result = promptForWords(tmpl);
        } catch (IOException e) {
            System.out.println("There was a problem prompting for words");
            e.printStackTrace();
            System.exit(0);
        }
        String results = tmpl.render(result);
        System.out.printf("Your TreeStory:%n%n%s", results);
    }

    /**
     * Prompts user for each of the blanks
     *
     * @param tmpl The compiled template
     * @return
     * @throws IOException
     */
    public String promptForWord(String phrase) {
      BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
      String word = null;
      while(word == null || mCensoredWords.contains(word)) {
        System.out.println("Please enter a word:  ");
        try {
            word = buffer.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
      }
     return word;
  }


    /**
     * 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) {

        BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Please enter a word:  ");
        String word = null;
        while(mCensoredWords.contains(word) || word == null) {
            try {
                word = buffer.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(mCensoredWords.contains(word)){
                System.out.println("Oops! That word is censored. Please enter another word");
            }
        }
            return word;
        }
    }
Craig Dennis
Craig Dennis
Treehouse Teacher

I see two things John. First, use the reader supplied in the Prompter class mReader. Make a new method in the prompter that prompts for the story (and uses the existing mReader).

Secondly, make sure that you prompt for the word when you prompt...how else will users know what to word to enter?

// How can you make this more specific?
System.out.println("Please enter a word:  ");

So I changed it to this

public String promptForWord(String phrase) throws IOException {
        System.out.printf("Enter a %s: ", phrase);
        String word = mReader.readLine().toLowerCase(); //take out .toLowerCase()
        while (mCensoredWords.contains(word)) {
            System.out.printf("That word is not allowed. Please enter another %s: ", phrase);
            String newWord = mReader.readLine().toLowerCase(); //take out .toLowerCase()
            word = newWord;
        }
        return word;
    }

but now I get "Bummer! Looks like you didn't present the TreeStory (3)"

output.html
Please enter the story template:  
Enter a first prompt: Enter a second prompt: