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

Amit Dhamankar
Amit Dhamankar
4,163 Points

What is wrong with this code?

Do I need to write the full code in this exercise. Or is there any bug in this code ? please help me solve this exercise? And guys one more thing,
I understand what is taught in the videos but I cannot start writing a program right from the beginning. How should I approach ? sorry if the question seems a bit wierd!!!!

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("No")) {
    console.printf("Learning do while loop is really good for landing in a MNC");
  }
}while(response.equals("No"));

1 Answer

Ryan Ruscett
Ryan Ruscett
23,309 Points

Hola

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.

String response;

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

if(response.equals("Yes")) {
    console.printf("Awesome!");
}

} while(response.equals("No"));

So I need to define the response value outside the do portion of the loop, but also inside it. So I define the variable outside the do portion. This allows the while portion to understand the input. If I put the entire thing outside the do like you have it. Then I set a value, but if that value isn't a set value. The while loop condition says true so keeps running. But when it runs the second time it doesn't prompt the user for another answer. It just runs what is in the do portion again. So in order to make the response variable available outside the do loop so the while portion can access it. It needs to define it outside the do portion but yet set it within the do portion. So now when I run it. If the while is true, it runs again. Which means it re-prompts the user for a value again. This happens until I say Yes. Then it sets the variable outside the do as yes. So the while portion also outside the loop can access the value and determine if it should run again or not.

I know it's a bit confusing and its late. Please let me know if you don't understand what I am saying and tomorrow I can make it more clear for you lol

Thanks!

Amit Dhamankar
Amit Dhamankar
4,163 Points

Thank you Ryan. It took me a very long time to solve this and you did it in jiffy. Thanx once again.

Ryan Ruscett
Ryan Ruscett
23,309 Points

No problem. You are on your way, keep it up!