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!

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


Lauren Lolo
Lauren Lolo
352 Points

Using "boolean" for more then one bad word?

I just watched the video about using boolean to block bad words in the program. I was just curious, but is there a way I can put in more bad words from being used in my program? (Blocking more words then just one?)

1 Answer

Rob Bridges
.a{fill-rule:evenodd;}techdegree seal-36
Rob Bridges
Full Stack JavaScript Techdegree Graduate 35,467 Points

Hey there Lauren,

This is certainly doable. We'd use much of the same code, first of which we'd just set a string with a list of bad words

Sting badWords= "jerk loser idiot imbecile moron"; 

including everything that we want into the list.

Next we would set our noun variable

console.println("Enter a noun");
String noun = console.readLine();

Now what we would want to do next is make sure that the word that we pass into noun isn't contained in the string badwords.

Thankfully we can do this by

boolean isBadWord = badWords.contains(noun.toLowerCase()); 

what this would do is it would turn our noun to lower case, and see if that word is contained in our String bad words.

Another way to set up the boolean would be

boolean isBadWord = badWords.indexOf(noun.toLowerCase()) > -1 

This would check what is passed in as a value against your list of bad words, and it would return true because it found the value indexed in the string, rather in the first index or any proceeding it.

Second we would pass in the boolean.

if (isBadWord) {
 console.println("Woah there, slow it down, no need for that language");

Thanks, if this doesn't help or you'd like further clarification let me know.