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 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.