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 trialHassan Osman
628 Pointsdont know the code
continually prompt user in do while loop, declare response outside loop. im really stuck on this
adsdev
15,583 PointsYour 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
adsdev
15,583 PointsWhat 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
Treehouse TeacherHassan, 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! :)
Hassan Osman
628 Pointswhich clue
Ben Jakuben
Treehouse Teacheramitsanchit '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
628 Pointsstring response;
response=console.readLine("do you understand while loops");
while(response.equlasIgnoreCase("no");
Ben Jakuben
Treehouse TeacherYou'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
628 Pointsthe 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
628 Pointsstill there?
Hassan Osman
628 PointsString response;
response=console.readLine("do you understand while loops?");
while(response.equalsIgnoreCase("no");
Hassan Osman
628 Pointsnot working
Ben Jakuben
Treehouse TeacherRight, 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
628 PointsString response;
do {"you understand while loops?");
while(response.equalsIgnoreCase("no");
Ben Jakuben
Treehouse TeacherThe 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
Treehouse TeacherRemember 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:
- Declare the
response
variable as a container forString
data. - Start the do/while loop with the
do
keyword and opening brace. - 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
). - Another comment line
- End the block of code that is executing in the loop with the closing brace. Add the
while
condition withwhile(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
628 Pointsthis is correct
Hassan Osman
628 Pointsso what is the code then
Ben Jakuben
Treehouse TeacherWe 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
628 Pointsso the code will be
String response=cosnsole.readLine("do you understand do while loops?");
do{
you understand
}while(response.equalsIgnoreCase("no");
Hassan Osman
628 Pointsguys iam offically done here
Hassan Osman
628 Pointsstill there
Hassan Osman
628 Pointsiam done with the second challnege, now iam starting the third challlenge
adsdev
15,583 PointsHassan 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.
adsdev
15,583 PointsHassan 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 :)
adsdev
15,583 Pointsadsdev
15,583 PointsHassan Osman , Which part are you doing of what course ?