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

William Roberts
William Roberts
564 Points

lost on java basics

i am on task 3 of 3 on the do while lesson and cant figure i out. using console.printf print a response? this is all i have so far....

String response; boolean isInvalidWord; do { response = console.readLine("Do you understand do while loops?"); isInvalidWord = (response.equalsIgnoreCase("No")); if (isInvalidWord) { console.printf("please try again. \n\n"); } } while (isInvalidWord);

Example.java
// I have initialized a java.io.Console for you. It is in a variable named console.
String response;
boolean isInvalidWord;
do {
  response = console.readLine("Do you understand do while loops?");
  isInvalidWord = (response.equalsIgnoreCase("No"));
  if (isInvalidWord) {
    console.printf("please try again. \n\n");
  }
} while (isInvalidWord);

2 Answers

Andrew Winkler
Andrew Winkler
37,739 Points

Do-while loops are nice because they don't need wordy if-then logic. You basically get to tell the computer to do a step infinite times unless some condition is met.

//Declare the variable;
String response;

//Define the DO:
do {
    response = console.readLine("Do you understand do while loops?  ");

//Define the WHILE:
} while(response.equalsIgnoreCase("no"));

//Define the rest:
//Java runs sequentially. This line won't be run until the prior condition is met.
console.printf("Because you said %s, you passed the test", response);
Ralph Mowers
Ralph Mowers
5,778 Points

Try this for question 3/3. } while (isInvalidWord); console.printf("Type in message between quotes. When you see <response> you want to use %s for string to return response value within in message");