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 - Looping Until the Value Passes

How can I make an array of strings of offensive words and use the contains method so i can test for multiple words.

This is my section of code, it apparently contains no errors but the loop doesn't work, if i type in one of the offensive words, the code just continue to on to enter and adverb etc. Here is the relevant section:

     boolean isInvalidWord;
     String word = "dork toss nerd";


     do {
       noun = console.readLine("Enter a noun:  ");
       // Boolean reads back eaither true or false
       // use isInvalidWord as a variable for all invalid words to stop repetition
       isInvalidWord = (noun.contains(word));

    // ban certain language being used in both lower and upper case. \\ means or.
       if(isInvalidWord) {
          console.printf("That language is not allowed! Try again. \n\n");
         }
       }  while(isInvalidWord);

     String adverb = console.readLine("Enter an adverb:  ");
     String verb = console.readLine("Enter a verb:  ");

     console.printf("My TreeStoryPrototype:\n---------------\n");
     console.printf("%s is a %s %s. They are always %s %s.\n", name, adjective, noun, adverb, verb);
}

}

1 Answer

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

Change the line:

isInvalidWord = (noun.contains(word));

To

isInvalidWord = (word.contains(noun));

You are checking that String word with bad words contains noun that user has entered.

In your code, you are checking that noun user entered contains word, which is not right, so it is like you are checking that word "dork" that you enter contains "dork toss nerd" which will obviously fail...

Cheers that helps a lot. Is there a way of making "dork toss nerd" into some sort of list "dork", "nerd", etc. so that all words that contain those specific words can be banned ie. "tosser"? At the moment i can only test if they put in one of the exact words or a word within one of the longer words eg. "tos"

Alexander Nikiforov
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Alexander Nikiforov
Java Web Development Techdegree Graduate 22,175 Points

I would say you can put your words in a List<String>:

List<String> censoredWords = new ArrayList<>();
censoredWords.add("dork");
// some lines later
censoredWords.contains(noun);

This way only exact match will be checked, i.e. tos will not pass, however in your method with String words = "dork toss", tos will be a censored word because String words = "dork toss" contains tos...

But that will not solve your problem of finding "tosser", when you set up "toss" as censored word.

In order to remove this particular problem, you can apply additional logic:

// cycle through words that you've put in a list "dork", "toss" one by one
for (String censoredWord : censoredWords) {
   // will be true if userInput = "tosser" and one of censored words is "toss"
   if (userInput.contains(censoredWord) {
       isInvalidWord = true;
       break;
   }
}