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

KRIS NIKOLAISEN
KRIS NIKOLAISEN
54,375 Points

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"));