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

can get through Challenge Task 2, please let me know if this is compiler's error or mine. thankyou

I think my code is without errors (from what i know:)) but it might be my arrogance, cause i am getting so excited during coding! :)

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

2 Answers

Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Hi Luka

You just missed coded some part of the task. I will point it out in your own code:

// I have initialized a java.io.Console for you. It is in a variable named console.
String response;
do { 
   response = console.readLine("Do you understand do while loops?    ");
   if (response ("no")); {//<-- this is unnecessary but if you insist you need to change it to be: if (response == "No") {
    //after if statement if (bla bla bla) you do not need ' ; ' you can just go straight to {}
    console.printf("try again");
  } while response("no");//<--This is wrong. It should be: } while (response == "No");
//remember it is "No" not "no" (read the task carefully)

I hope this can help a little.

thanks it was helpful. I corrected my code. it seemed that i was lacking understanding of a do while loops

Dave Harker
PLUS
Dave Harker
Courses Plus Student 15,510 Points

Hi Luka,

I'd suggest taking a look at the challenge requirements again. I just ran through that challenge to see what the steps were and had no issues, but I can tell you that your code there does not address the requirement of task 2.

Task one is just to prompt the user with the questions and assign into String variable response, which you seem to have done. Task two requests that you:

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.

So move the declaration out and assign it a default "No" response
Then build the do while loop.. so

String response = "No" // declare outside, I'd assign it a default state of whatever the loop condition is (in this case "No" to loop)
do {
  get user input and assign to response variable;
} while (condition);

Re: other code you've got ... You've got an unnecessary semicolon ( ; ) in that if statement, and never close it off either. ( } ) You also don't need this if statement within the do/while, it's not asked for ... or the printf inside the do/while either ... so, I'd ditch them.

Challenge tasks are pretty specific. You're on the right track but just be careful of syntax and making sure to complete exactly what is asked for.

Happy coding, :dizzy:

Dave