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

do while loop

got stuck for two days please help any one

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?" );
//String noun;
boolean isInvalidWord;

    do{
   // noun = console.readLine("Enter a noun:  ");
      isInvalidWord=(response.equalsIgnoreCase("No"));
    if(isInvalidWord){
      console.printf("Do you understand do while loops?\n");
      }
    }while(isInvalidWord);
Matt Nickele
Matt Nickele
468 Points

at what point does it not work?

it worked finally but it shows error of communication problem dont know wats the problem .

2 Answers

Sandeep, you can just test in the condition of the do-while loop:

String response;

do {
  response = console.readLine("Do you understand do while loops?");
} while (response == "No");
console.printf("Because you said %s, you passed the test!", response);

Here you create the variable, outside the do while loop so it stays in scope for the printf statement after the loop. Then you ask the user the question, and store the answer in the response variable. Then you check to see if the response is No or not, in the condition (response == "No").

Qui Le
Qui Le
10,998 Points

Hi Sandeep, I have seen your code, and I am not sure if you got the solution. I'll like to explain how to make your code executable.

// I will use your variable, noun

String noun;

do {

  // Read in user's reply

  noun = console.readLine("Do you understand do-while loops? Enter Yes or No: ");

} while (noun == "No"); // Loop will repeat until noun is different from "No"

console.printf("You replied with %s. So, you have understood do-while loops", noun);