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

priya tumu
priya tumu
367 Points

do while loop question no 2

Question - Now continually prompt the user in a do while loop. The loop should continue running as long as the response is No. Don't forget to declare response outside of the do while loop

code 1) String response = console.readLine("Do you understand do while loops?"); do{ if (response.equals("yes")){ console.printf("do you understand do while loops?"); } }while (response.equals("No")); Error:Code took longer to run

code 2)String response = console.readLine("Do youuderstand : ");; boolean isInvalidWord; do {

    isInvalidWord = (response.equalsIgnoreCase("yes"));
    if (isInvalidWord){
      console.printf("do you understand  : ");
    }
  }while (isInvalidWord);

Error:Continuous looping does not end until i press cntrl+c

If somebody could help me understand what mistake am doing.

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?");
do{
  if (response.equals("yes")){
    console.printf("do you understand do while loops?");
  }
}while (response.equals("No"));
Dave StSomeWhere
Dave StSomeWhere
19,870 Points

How is your response ever going to be set to "No" if you don't prompt for a new response within your loop?

Do you see it now?

1 Answer

It looks like you have created an infinite loop.

You need to ask the user for the input otherwise the answer will always be no.

(I'll update this answer if you need additional help. )