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

Denis Hajdari
Denis Hajdari
404 Points

Task 3/3

I don't know what i am doing wrong, i am trying to print after the loop finish , and when it finish the variable response would hold the "Yes" and so i concatenate the response string with the default text. Any help would be appreciated.

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.equals("No"));
 console.printf("Because you said " + response + ", you passed the test!");

2 Answers

Denis, your code is looking good but it has one little problem because of it you can't pass the task 3.

There are two points I need to highlight:

  1. Always "initialize" a variable with a value before using it. Initializing means inserting it with a valid value. for example, String response = ""; instead of String response;

  2. Console.printf works a bit different and what you wrote will work perfect for "System.out.print()", but for a console.printf you need to specify a format for the string to be printed on console. e.g You have a variable

String text = "Hello";

Now in order to print it using console.printf , you will use "%s"

console.printf("%s World",text);

During runtime , it will put the value of text variable in the place of %s.

So your solution for this task is following:

// 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.equals("No"));

console.printf("Because you said %s,you passsed the test!",response);
Yanuar Prakoso
Yanuar Prakoso
15,196 Points

Hi Denis...

since you are using console.printf you might want to consider using format specifier placeholder "%s" for String instead of concatenation of strings. This is how you should write your last code:

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

I hope this can help you a little