Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

Hassan Osman
Hassan Osman
628 Points

dont know the code

continually prompt user in do while loop, declare response outside loop. im really stuck on this

Hassan Osman , Which part are you doing of what course ?

Your forum question says Android, But you're talking about an Java code challenge. Next time put it in the right category. You got me a little confused because of that xD

14 Answers

What i think you should do is

public static void main(String[] args){
        do {
            //prompt code(s);

        } while (condition is false);
 //Outside of the loop, print response
}

something like this Hassan Osman ?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hassan, this clue should help you get past this step in the code challenge. If you are still having trouble, let us know what code you are trying and where you are stuck and we'll try to help! :)

Ben Jakuben
Ben Jakuben
Treehouse Teacher

amitsanchit 's sample code above should give you a template of what to try. Paste in here the exact code you are trying with specific details about what you don't understand and we can go from there.

Hassan Osman
Hassan Osman
628 Points
string response;
response=console.readLine("do you understand  while loops");
while(response.equlasIgnoreCase("no");
Ben Jakuben
Ben Jakuben
Treehouse Teacher

You're on the right track here. #1, don't forget that String needs a capital S.

You want to put your 2nd line in the body of a do/while loop. In the example above, replace //prompt code(s); with your line of code.

Your 3rd line will be the while line in the do/while loop.

Try it out and paste your new code in here if you continue to have trouble. :)

Here's a link to the challenge for anyone who visits this page later.

Hassan Osman
Hassan Osman
628 Points

the question is now continually prompt the user in do while loop. the loop should continue running as long as the response is no. dont forget to declare the response outside of the do while loop.

Hassan Osman
Hassan Osman
628 Points
String response;
response=console.readLine("do you understand while loops?");
while(response.equalsIgnoreCase("no");
Ben Jakuben
Ben Jakuben
Treehouse Teacher

Right, you only addressed the first thing that needs to be fixed. You need to use the do/while format from the video and the earlier hint above for the rest of your code. Read my previous hint line by line and make sure you are putting the pieces in the right place. Here's a further hint: After the String response; line, start your do/while loop by simply putting do on its own line:

String response;
do {
Hassan Osman
Hassan Osman
628 Points
String response;
do {"you understand while loops?");
while(response.equalsIgnoreCase("no");
Ben Jakuben
Ben Jakuben
Treehouse Teacher

The structure of the do/while loop needs to be like this:

do {
  // Code to execute in the loop
} while(condition);

Your condition is correct, so your shell should look like this:

do {
  // Code to execute in the loop
} while(response.equalsIgnoreCase("no");

The code you want to execute is simply the line from step 1 that sets response from the console, which you had correct earlier as: response=console.readLine("do you understand while loops");

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Remember that the syntax is really important here. I've pushed my hint even further below (and added line numbers) and will walk through the code line by line:

  1. Declare the response variable as a container for String data.
  2. Start the do/while loop with the do keyword and opening brace.
  3. Lines 3 and 4 are hint comments that should be replaced by your own code. You just need one line of code between the curly braces (your earlier line that sets response).
  4. Another comment line
  5. End the block of code that is executing in the loop with the closing brace. Add the while condition with while(condition);
01 String response;
02 do {
03   // Code to execute in the loop (in other words - set 'response' here
04   // with the same code you used in step 1 to set 'response'.)
05 } while(response.equalsIgnoreCase("no");

Between this and the video you should definitely be able to figure this one out! :)

Hassan Osman
Hassan Osman
628 Points

so what is the code then

Ben Jakuben
Ben Jakuben
Treehouse Teacher

We try really hard to not give the code straight out. :) My last comment is super close to the answer, so if you follow along closely you'll get there!

Hassan Osman
Hassan Osman
628 Points

so the code will be

String response=cosnsole.readLine("do you understand do while loops?");
do{
you understand
}while(response.equalsIgnoreCase("no");
Hassan Osman
Hassan Osman
628 Points

guys iam offically done here

Hassan Osman
Hassan Osman
628 Points

iam done with the second challnege, now iam starting the third challlenge

Hassan Osman Don't give up so easilly :P ! Think critically about the challenge. I can say of personal experience its the best way to learn how to code. Programming requires a kind of mind set. The code challenges really help in applying the so to speak theory.

Hassan Osman I refinished the code challenge so i have a better understanding of the objectives. If there's any problem you are encountering or things you don't quite understand, please state em :)