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 - Using Logical ORs

Simplifying the Logic

When Craig uses or's to censor out dork and jerk, instead of writing

noun.equalsIgnoreCase("dork") || noun.equalsIgnoreCase("jerk")

in his if statement, could he write

noun.equalsIgnoreCase("dork" || "jerk")

and on and on and on if we have more things to censor? I just feel like rewriting the entire variable and method is kind of bulky if we have more things we'd like to check for.

2 Answers

Josip Dorvak
Josip Dorvak
18,126 Points

That would not work since ("dork" || "jerk") isnt a String but a Boolean (which is a true or false value). Later on he'll probably get to loops and arrays. The preferred way is to have an array of words (an array is a variable that can hold many variables) and loop through all Strings in the array and compare the noun to those.

when we have more strings to censor may be we could add all the strings into a different data structure like a vector<Strings> and loop thru to find out if the particular string has to be censored.