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

Why is my code not working?

How to I use console.printf to write a formatted string? I've done everything like the previous video showed me and it still isn't working.

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?"); 
  while (response.equalsIgnoreCase("No"));
if (response.equalsIgnoreCase("No")) {
console.printf("Because you said <response>, you passed the test!");
    }
}

Try this

do{
response = console.readLine("Do you understand do while loops?");  // "yes" or "no" is stored in the response variable 
// when someone types "no" the loop repeats
// when someone types "yes" the loop is exited
}while(response.equalsIgnoreCase("no"));

//After the loop exits, the following line prints to the console
console.printf("Because you said %s, you passed the test!",response); //task 3console.printf("Because you said <response>, you passed the test!"); 
}

1 Answer

Richard Lambert
PLUS
Richard Lambert
Courses Plus Student 7,180 Points

Hello mate,

The correct format for the do-while statement can be found below. Comments included are points to note when creating this type of statement.

do {
    /**************************************************************************
    Variables declared within this code block (within the curly braces) are 
    not accessible to any code outside the block, including the conditional 
    expression of the do-while statement. They are said to be out of scope. 
    **************************************************************************/

    /**************************************************************************
    One statement or multiple statements can be placed within this code block.
    **************************************************************************/

    /**************************************************************************
    Statements that are to run with each iteration of the do-while statement 
    are to be placed within this code block.
    **************************************************************************/

    /**************************************************************************
    Statements placed within this code block will run at least once.
    **************************************************************************/

    /**************************************************************************
    Typically, a statement will be placed within this code block which alters 
    the state of an externally declared variable. This variabe can then be 
    accessed by the conditional expression of the do-while statement, which 
    must evaluate to true or false, determining whether looping continues.
    **************************************************************************/

} while(expression);    /*This can be any expression evaluating to true or false*/

Hope this helps