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 trialAndy Duncan
8,292 PointsI cannot get my Java code past your test
Although I have written a piece of Java code which works (or seems to work) perfectly well in IntellijIDEA, I cannot get it past your test system. I keep 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: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:65) at com.teamtreehouse.Prompter.promptForWords(Prompter.java:47) at com.teamtreehouse.Prompter.run(Prompter.java:35) at com.teamtreehouse.Main.main(Main.java:21) at JavaTester.run(JavaTester.java:77) at JavaTester.main(JavaTester.java:39)
I do not ever get this error in IntellijIDEA
Here is my Main code:
package com.teamtreehouse;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Arrays; import java.util.List;
public class Main {
public static void main(String[] args) {
Prompter prompter = new Prompter();
Template tmpl = null;
System.out.printf("Please input a new template story: ");
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String story = reader.readLine().trim();
tmpl = new Template(story);
} catch(IOException iox){}
prompter.run(tmpl);
}
}
Here is my Prompter.java code:
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> results = null;
try {
results = promptForWords(tmpl);
} catch (IOException e) {
System.out.println("There was a problem prompting for words");
e.printStackTrace();
System.exit(0);
}
System.out.printf("Your TreeStory:%n%n%s", tmpl.render(results));
}
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;
}
public String promptForWord(String phrase) throws IOException {
String result = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
boolean badWord = true;
while(badWord) {
System.out.printf("Please input %s: ", phrase);
String input = reader.readLine();
if(input == null || input.length() < 1){
System.out.println("Please enter some input");
} else {
badWord = (mCensoredWords.contains(input));
if (badWord) {
System.out.printf("%s is a bad word %n", input);
} else {
result = input;
}
}
}
return result;
}
}
I want to get this module done so I can carry on learning Java. How do I get past this error?
package com.teamtreehouse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;
public class Main {
public static void main(String[] args) {
Prompter prompter = new Prompter();
Template tmpl = null;
System.out.printf("Please input a new template story: ");
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String story = reader.readLine().trim();
tmpl = new Template(story);
} catch(IOException iox){}
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> results = null;
try {
results = promptForWords(tmpl);
} catch (IOException e) {
System.out.println("There was a problem prompting for words");
e.printStackTrace();
System.exit(0);
}
System.out.printf("Your TreeStory:%n%n%s", tmpl.render(results));
}
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;
}
public String promptForWord(String phrase) throws IOException {
String result = "";
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
boolean badWord = true;
while(badWord) {
System.out.printf("Please input %s: ", phrase);
String input = reader.readLine();
if(input == null || input.length() < 1){
System.out.println("Please enter some input");
} else {
badWord = (mCensoredWords.contains(input));
if (badWord) {
System.out.printf("%s is a bad word %n", input);
} else {
result = input;
}
}
}
return result;
}
}
# 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
4 Answers
Steve Hunter
57,712 PointsHi Andy,
That's very strange - I can't see anything immediately but that's a lot of code to skim through. Do you have a GitHub account you can push this up to. I can then pull it down to replicate the error here. Not to worry, if not.
One thing; and I have no idea if this could be the root of your problem. You're running out of memory. I wonder if having two stream readers is contributing to that?
Inside your Prompter
constructor, you have created a reader called mReader
. Inside promptForWord
you don't use that, but you create another one, reader
. Maybe that's causing the memory leak? Perhaps try using the mReader
that's available to the whole class?
Steve.
Craig Dennis
Treehouse TeacherHi Andy!
Instead of making a new BufferedReader, can you use the one from inside the Prompter class. I think it's tripping up my "test". Still working out the kinks in this one :/
Thanks for the patience and sorry for the frustration!
Andy Duncan
8,292 PointsThanks Steve, managed to get past the memory buffer problem! :-D
Steve Hunter
57,712 PointsNo problem! Glad you got it sorted.
Alan Brown
20,524 PointsJust for reference. I kept running into this OutOfMemory error too. I wrote a few variations of while loops and ran into this usually when trying to code around NullPointException errors (which are not thrown in IntelliJ).
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsIt looks as though all roads lead to
promptForWord
as being the cause of this issue. Just by way of comparison; here's my method: