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 trialNeil Gordon
8,823 Pointshaving a little challenge with the syntax and methodology for a do while loop
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.
// I have initialized a java.io.Console for you. It is in a variable named console.
String response;
do {
String response =console.readLine("Do you understand do while loops ");
while response ("no");
}
7 Answers
Kym Bryan Carasig
Courses Plus Student 1,013 PointsThis is your code.
do {
String response =console.readLine("Do you understand do while loops ");
while response ("no");
}
no problem in your code except for 1 thing. You need to put while outside of your do{ }. Like this.
Solution:
do {
String response =console.readLine("Do you understand do while loops ");
}while response ("no");
Hope i helped.
Neil Gordon
8,823 Pointsah , wow okay got ya thanks for the help going to give it a shot
Neil Gordon
8,823 Pointsdo {String response=console.readLine("Do you understand do while loops?");
}
while response("no");
JavaTester.java:124: error: '(' expected while response("no"); ^ JavaTester.java:124: error: ')' expected while response("no"); ^ 2 errors
I honestly no longer see the error thank you KYM for the help on my syntax. can you tell me how the syntax is wrong iin my new post?
Kym Bryan Carasig
Courses Plus Student 1,013 PointsYou should declare the String response variable outside of the do{ } because if you declare it inside do{ } they wont be able to reach it when you go outside of your do code block{ }. And also your while code. it should be response.equals("no") since its a string and should be after while code and inside ( ). try to write it like this.
String response;
do{
response = console.readLine("Do you understand do-while loops?");
} while(response.equals("no"));
Neil Gordon
8,823 Pointsthank you for your help again , i tried that and deleted the string variable so many times I should not have second guessed myself.
Kym Bryan Carasig
Courses Plus Student 1,013 PointsHappy to help you sir. Sorry i can't explain that well, as english isn't my native language. Happy coding!
Neil Gordon
8,823 Pointsyou're doing an amazing job, thanks! the errors are helping me learn. I also made the mistake of not taking out the second string. another learning tool .
Steve Hunter
57,712 PointsEdited your code blocks for clarity.
For info, use three backticks then the language name, closing the code block with another three backticks.
Leave a line clear before and after. Edit one of your posts to see how it looks. It should come out like below ...
// this is a Java comment
BlogPost blogPost = new BlogPost();
Steve.
Kym Bryan Carasig
Courses Plus Student 1,013 PointsKym Bryan Carasig
Courses Plus Student 1,013 Pointsi dont know why the page formating isn't working. That should highlight your code.