Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Basics Perfecting the Prototype Censoring Words - Looping Until the Value Passes

If you tell it in while (isInvalidWord); if you told it inside the brackets

why do you have to tell it those words are bad twice? Inside the bracket and next to the while keyword.

The condition inside the while loop is used to display the message "invalid noun" only. The while condition is used to let the user enter a new noun.

At first, I thought the same. After all, if the while-statement says that the word is invalid, why check it again in the if-statement? However, the check in the if-statement is necessary, yet ONLY for the first run. (So during the 'do').

If you would not check in the if-statement, running the code would always state that 'That language is not allowed.' and that you have to try again, even when you entered something correct. If you would not check this in the while-statement, you get back at the previous situation, where you will have to System.exit(0) your code, or allow the bad language anyway.

So, even though this results in a repeating of the invalidWord-check (√°fter a bad entry!), trying to get this out will result in repeating something even bigger. (Both the lines for entering a noun and the setting of isInvalidWord.)