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

Osman Mukhwana
PLUS
Osman Mukhwana
Courses Plus Student 488 Points

v

I am running into the following error, while, trying to prompt the user continually to give a different response from "No" using the do while loop:

java.lang.OutOfMemoryError: Java head space

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.

String response = console.readLine("Do you understand do while loops?");

//boolean invalidResponse = (response.equals("No"));

do{
    if (response.equals("No")){
    console.printf("Please enter another response");
    }
}while(response.equals("No"));

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Osman Mukhwana! I received your request for assistance. Derek is correct. You have set up what could potentially be an infinite loop which is why it says it's running out of memory. You start by declaring a variable named response and setting it to what the user types in. But then you never give the user a chance to enter another value. You print out a prompt for it, but there's not a line inside your do while loop that actually reads in their new response. So response will only ever be whatever they first put in.

Try this:

  • Declare response to hold an empty string
  • Inside the do read in the response from the user and set response equal to what the user types in. This will overwrite the currently empty string inside of response
  • Then check if the user's response was "No"

Hope this helps, but let me know if you're still stuck! :sparkles:

First off I would change the title of this forum thread "V" just isn't acceptable. Secondly, you need to put the readLine statement inside of the "Do" portion of the loop, and delete the if-statement, It's completely redundant seeing as how you're already checking for a response in the "While" portion of your loop. With those changes I'm sure it will pass.