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

Jack Cummins
Jack Cummins
17,417 Points

I have a do while loop and I put a string variable in it. It says variable declaration not allowed here.

In the video they didn't show an example of a do while loop.

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 while loops  ?");

  boolean isNo=(response. equalsIgnoreCase("no"));





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

1 Answer

michaelcodes
michaelcodes
5,604 Points

Hi there! So at the very top, we only want to declare the variable, not fill it with anything yet:

String response;

We do not need to use a boolean variable, we can use response.equalsIgnoreCase as our condition to run

  //boolean isNo=(response. equalsIgnoreCase("no"));

The last part is for the do-while loop we must use curly braces around the conditons as such:

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

Hope this helps, take care and happy coding

Jack Cummins
Jack Cummins
17,417 Points

It worked! Thank you so much for the help! I gave you a best answer!

Jack Cummins
Jack Cummins
17,417 Points

I also gave you an upvote!