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

Gregory Heard
Gregory Heard
1,975 Points

Do while loops

Already completed the excercise once, but going back over everything to make sure I understand and I can't work this out. A basic explanation on do while loops in this situation would help loads. Thanks in advance!

Greg

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String question = console.readLine("Do you understand do while loops?");
String response = question;

boolean yesAnswer;
String response;

  do {
    String question = console.readLine("Do you understand do while loops?");
    String yesAnswer = response.equalsIgnoreCase(response);
    if (response == "yes") {
      console.printf("CONGRATS");
      System.exit(0);
    }
  }
    while (Response == "no");

2 Answers

Kevin Faust
Kevin Faust
15,353 Points

hi. i wrote notes below. let me know if it makes sense

String response; //initialize a variable called "response"

do {
   response = console.readLine("Do you understand do while loops?"); //ask the user if they understand loops and store
 //their answer in the response variable which we initialized at the top
} while (response == "No");  

//now what we do here ^, is check if the user typed in "No". if the user typed "No", then we want to run the loop again and
// reprompt the user again and keep looping until they type in anything other than "No". you wrote down an if statement in
 //your code above which is unncessary because this in itself has the same role as an if statement

console.printf("Because you said %s, you passed the test!", response); //print out message to console with user's repsonse
Gregory Heard
Gregory Heard
1,975 Points

Thank you very much Kevin! Very helpful. I do have a habit of making things more complicated than they have to be!

Cheers,

Greg