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

Máté Batik
Máté Batik
1,650 Points

Don't know where is the problem :(

where is the error here?

Example.java
String response;
Boolean isInvalidWord;
do {
  response = console.readLine("Do you understand do while loops?");
  isInvalidWord = (response.equalsIgnoreCase("No"));
  if (isInvalidWord) {
  console.printf("Try!");              
  } while (isInvalidWord);
  Sysytem.exit (0);
}
foxtails
foxtails
3,655 Points

Hi, checked the exercise. You don't need System.exit(0) there at all as that will exit from the program. Also the answer you are printing is different from challenge. It's "Because you said <response> you passed the test!". Which means you have to use string variable %s, when printing the text. So unless we are looking at two totally different challenges, it should look something like this:

String response;
boolean isInvalid;
do {
  response = console.readLine("Do you understand do while loops?  ");
  isInvalid = (response.equals("No"));
} while(isInvalid);
console.printf("Because you said %s, you passed the test!", response);

Sorry, I have no idea yet, how to make screenshot, so it's clearer, but hope it will help!

[MOD: edited code block]

2 Answers

Hi there,

Foxtails has identified where you've gone a little astray with this one! However, this task can be simplified still further.

You start by creating a string variable named response, as you have done. Then you enter a do:while loop. As soon as you enter the loop, prompt the user using the line from the first task; you have done this too.

You now have the user input. This is what the while loop is looking for. It wants to continually loop while the response is "No". So, the condition inside the while statement can be response.equals("No"). This removes the need for boolean variables. Just test the response; if it is "No", loop, if not - go to task 3. :smile:

String response;
do {
  response = console.readLine("Do you understand while loops?: ");
} while(response.equals("No"));

// task 3 goes here - the initial post hadn't reached that yet.

I hope that makes sense.

Steve.

P.S. - points of note/detail. The question spcifically asks to test against "No"; capitalised. Yes, you could catch other user input by using equalsIgnoreCase but that's not required of the question - it works, is probably best practice, but isn't needed here. Next, naming variables/methods 'invalid' is fine, but only if the response is invalid - "No" is a sensible, valid answer to your question - adopting clear naming conventions comes later in the course - just something to think about for now. :wink: :+1:

foxtails
foxtails
3,655 Points

You are welcome! Doing a great job here, keep coding!