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 trialBlessing Chiwaura
18,037 Pointshow to rectify Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
// I have initialized a java.io.Console for you. It is in a variable named console.
String response =console.readLine("Do yo understand do while loops?");
if (response.equals("No")) {
do {
console.printf("answer again");
} while(response.equals("No"));
}
7 Answers
Jeremy Hill
29,567 PointsString response = "";
do{
response = console.readLine("Do you understand do while loops?");
}while(response.equalsIgnoreCase("No"));
Jeremy Hill
29,567 PointsWe corrected your code on your other post but I will point out that the reason you are getting OutOfMemoryError is because this code causes your computer to go into an infinite loop; the reason for this is the first line of code you prompt the user for a response and the answer is stored but it is never again asked in the loop so the program keeps looping while looking for a condition that would make the statement false, which doesn't happen. You need to declare the String response outside of the do while loop and than continue to prompt the user inside the loop so eventually the program will get a different response from the user and the loop will end.
Blessing Chiwaura
18,037 Pointsok thank you. the problem now is string response is supposed to be declared outside the do while loop. how do i return then the response if you can help there
Blessing Chiwaura
18,037 Pointsthat code is returning the answer from first task does not parse
Jeremy Hill
29,567 PointsIf you are still having trouble with it let me know which task you are on and paste all of your code and I will help you.
Blessing Chiwaura
18,037 Pointsthats what i did so when i try to move on to task 2 and use the code as give it will variable arready declared
Jeremy Hill
29,567 PointsYou can't have it where it shows String response twice- after you declare it the first time you no longer have to have the type String in front of it anymore; the compiler will bark at you for it.
Jeremy Hill
29,567 PointsI will show you each step to make sure that you got it: Task 1:
String response = console.readLine("Do you understand do while loops?"); // Declared and initialized
Task 2:
// I have initialized a java.io.Console for you. It is in a variable named console.
String response = "";
do{
response = console.readLine("Do you understand do while loops?"); // added String response but removed declaration: it is only needed once
}while(response.equalsIgnoreCase("No"));
Task 3:
// I have initialized a java.io.Console for you. It is in a variable named console.
String response = "";
do{
response = console.readLine("Do you understand do while loops?");
}while(response.equalsIgnoreCase("No"));
console.printf("Because you said %s, you passed the test", response);
Blessing Chiwaura
18,037 Pointsthanks Jeremy you knocked sense to my head i finally got it
Jeremy Hill
29,567 PointsOkay good ;)
Jeremy Hill
29,567 PointsJeremy Hill
29,567 PointsThe first task wants you to declare and initialize String response like this:
String response = console.readLine("Do you understand do while loops?");
That is all you have to do for task 1.
Jeremy Hill
29,567 PointsJeremy Hill
29,567 PointsTask 2 you have to place the response = console.readLine("Do you understand do while loops?"); inside the do while loop. but you have to go back up and delete it from your String response declaration and just leave it with just the double quotes like I showed you.