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

Vincent Karcz
Vincent Karcz
307 Points

I don't know what I'm doing --- Code takes too long to run

I do not understand how to set up this -- do while -- code. I've watched the video four separate times, and have looked to other resources. I just can not figure this out. The error I got previous to "Code took too long to run" was "Make sure you are looping while the value is set to No". Please help me understand what I'm doing wrong.

Example.java
String response = console.readLine("do you understand do while loops?");
boolean userUnderstands = false;
  do {
    userUnderstands = response.equalsIgnoreCase("no");

  }while (userUnderstands == true);

1 Answer

In your loop you will want to update response with user input and check that response so there is an opportunity to exit the loop. With your code:

userUnderstands = response.equalsIgnoreCase("no");

only considers the initial response. If the initial response is 'no' you have an infinite loop.

Introducing userUnderstands is more than needed. Making the following adjustments passes the task:

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

  while (response.equalsIgnoreCase("no"));