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

Santos Litkie
Santos Litkie
6,172 Points

I have no clue how to do this properly!

It keeps saying there is an error on line six. Not sure what I am doing wrong.

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 loops?  ");
  isInvalidWord = (response.equalsIgnoreCase("No"));
                     if (isInvalidWord) {
                       console.printf("Try again! \n");
                     }
                     } while (isInvalidWord);

1 Answer

Michael Hess
Michael Hess
24,512 Points

Hi Santos,

Everything is looking good, except you don't need the if statement inside the do-while block.

Please see the following code:

String response;
boolean isInvalidWord;
do {
  response = console.readLine("Do you understand loops?  ");
  isInvalidWord = (response.equalsIgnoreCase("No"));


 } while (isInvalidWord);


console.printf("Because you said %s, you passed the test!", response);  // task 3

I removed the if-statement and added task 3 -- the above code is a solution for the challenge. If you have any questions don't hesitate to ask!

Philip Schultz
Philip Schultz
11,437 Points

Hello Michael, You would have to make an if statement to let the user know that they entered the wrong value, right? Otherwise the code would keep repeating and not telling the user why it is. Can you display an error message in this case without using an if statement. Is this a good example to use a switch statement?

Michael Hess
Michael Hess
24,512 Points

Phillip,

No, the do-while loop takes care of letting the user know they entered the wrong value. If the user enters "yes" the do-while loop is exited and the printf statment is printed to the console.

if-statements and switch statements are not necessary to use to complete this challenge.

I have used the following solution, in previous posts, to explain what is happening in the code.

//Declare a String variable named response, then initialize it using double quotes
String response ="";


// The do while loop keeps asking the same question until "yes" is entered
//So we want to keep repeating when "no" is typed
// Exit the loop when "yes" is entered

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 3

If you have any questions -- feel free to ask!