Bummer! You have been redirected as the page you requested could not be found.

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

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

Hey!

Need your help. I was struggling this problem a lot, however, I got no improvements.

In my IDE everything works well, but when I upload it in the code challenge in throws "Exception in thread "main" java.lang.OutOfMemoryError: Java heap space"

My code is below: Main:

package com.teamtreehouse;

public class Main {

    public static void main(String[] args)  {
        // write your code here
        Prompter prompter = new Prompter();
        // Collects a template for the story and creates a template correctly formatted.
        Template tmpl = new Template(prompter.promptForStory());
        // Collects words for every empty space in story
        prompter.run(tmpl);
    }
}

Prompter:

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

    public String promptForStory() {
        System.out.print("Enter a story with words to be changed in format \"__noun__\":  ");
        BufferedReader mReader = new BufferedReader(new InputStreamReader(System.in));
        String story = "";
        try {
            story = mReader.readLine();
        } catch (IOException e) {
            System.out.println("There was an error while entering your story template.");
            e.printStackTrace();
            System.exit(0);
        }
        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<>();
        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;
        boolean isRestricted;

        do {
            System.out.printf("Enter %s:  ", phrase);
            word = mReader.readLine();
            if (word == null
                    || mCensoredWords.contains(word.replaceAll("\\s", "").toLowerCase())
                    || word.length() == 0) {
                isRestricted = true;
                System.out.println("You entered an invalid word. Try again.");
            } else {
                isRestricted = false;
            }
        } while (isRestricted);

        return word;
    }
}

StackTrace:

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.println(PrintStream.java:806)
    at com.teamtreehouse.Prompter.promptForWord(Prompter.java:96)
    at com.teamtreehouse.Prompter.promptForWords(Prompter.java:72)
    at com.teamtreehouse.Prompter.run(Prompter.java:38)
    at com.teamtreehouse.Main.main(Main.java:11)
    at JavaTester.run(JavaTester.java:77)
    at JavaTester.main(JavaTester.java:39)

Please help!

Hi, I had the same issue. For me using a Scanner instead of BufferedReader helped.

mReader = new Scanner (System.in);