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 Java Basics Perfecting the Prototype Looping until the value passes

Ara Brutian
Ara Brutian
565 Points

I'm getting "Exception in thread "main" java.lang.OutOfMemoryError: Java heap space" while creating a do/while loop.

I'm getting "Exception in thread "main" java.lang.OutOfMemoryError: Java heap space" while creating a do/while loop.

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
console.readLine("Do you understand do while loops?  ");
String response = console.readLine("Do you understand do while loops?  ");
boolean isNegation;
do {
  isNegation = (response.equals("No"));
  if(isNegation){
   console.printf("Try Again \n\n"); 
  };
} while(isNegation);

3 Answers

Raffael Dettling
Raffael Dettling
32,999 Points

You overthinked a bit :) But itΒ΄s good that you used equals instead of ==. You donΒ΄t need that first console.readLine("Do you understand do while loops?") line the do while loop will run at least one time and will assign the typed value to the response variable.

String response = "";
do {
  response = console.readLine("Do you understand do while loops?");
} while(response.equals("No"));
Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You have inadvertently created an infinite loop in the case where someone enters "No". You have two separate console.readLine() statements. The first one reads in the response and throws it away. It's never assigned anywhere. The second one reads in a response and assigns it to response. If the user now types "No" then isNegation will evaluate to true and it will print out "Try again". And the loop will start over. The problem here is that you've not included a way for them to change the value of response because your console.readLine is outside the loop. It will just continue looping and printing out "Try again" forever.

Here's how I did it:

String response = "";  //create an empty string

do {
  response = console.readLine("Do you understand do while loops?  ");  // Ask the user if they understand and set the response variable to their response
} while(response.equals("No"));  // If they respond with "No", run the loop again

I'll leave step 3 up to you because I am sure you can do it! Hope this helps! :sparkles:

Raffael Dettling
Raffael Dettling
32,999 Points

Thanks totally forgot the empty string^^

Ara Brutian
Ara Brutian
565 Points

Thanks, Jennifer, Raffael. This really helped.