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

Writing a string containing a list of invalid words

I am working through the extra credit and am defining a list of bad words to block. I have it setup as String badWords = "word1" and then have the the invalid words setup under nouns as isInvalidWord = (noun.contains(badWords))

How can I setup multiple words on the badWords string?

        String noun;
        String badWords = "nerd";
        boolean isInvalidWord;
        do {
          noun = console.readLine("Enter a noun:  ");
          isInvalidWord = (noun.contains(badWords));
          if (isInvalidWord) {
            console.printf("That language is not allowed.  Try again.  \n\n");
          }
        } while(isInvalidWord);  

3 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

I was imagining something relatively more easy, like this:

String badWords = "dork jerk nerd dweeb";
boolean isInvalid = badWords.contains(noun.toLowerCase());

That make sense?

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

Hey there Shane,

Unfortunately a string can only hold the one value. It hasn't been covered yet, but what you need is an array, this is data type that can hold multiple objects of the same type.

Let's keep your name the same and call the array badWords

String[] badWords = {"dork", "loser", "dummy" }; 

Follow the above syntax adding on as many as you would like to the array, just remember the comma between them until the last one.

That declares a string array, just to give a bit more input on the syntax, let's say you wanted an array of integers, it would be

int[] intArray = {1, 2, 3, 4, 5};

Again we could take on as far as we want to.

I know that Craig is going to cover arrays much more in this upcoming task, but for starters that would do it.

Thanks!

Hey, Rob. I am getting the following error on console when I make the changes.

TreeStory.java:27: error: incompatible types: String[] cannot be converted to CharSequence
isInvalidWord = (noun.contains(badWords));
^
Note: Some messages have been simplified; recompile with -Xdiags:verbose to get full output
1 error

The code looks like below

        String noun;
        String[] badWords = {"nerd", "jerk", "butthead" };
        boolean isInvalidWord;
        do {
          noun = console.readLine("Enter a noun:  ");
          isInvalidWord = (noun.contains(badWords));
          if (isInvalidWord) {
            console.printf("That language is not allowed.  Try again.  \n\n");
          }
        } while(isInvalidWord);  

Is there an issue going from an array and using (noun.contains(badWords))? Thanks for the help!

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

Hey again Shane,

We are definitely running away form the context of what the course wanted to reach on this. Looping through an array is new information, but I'll give you some helpful hints, to loop through an array the code is like this.

for (int i = 0, i < badWords.length, i++) {
   if (noun.equalsIgnoreCase(badWords[i])) {
      isInvalidword = true;
  }
}

What this does is loops through all of the words that are contained in your array badWords, and if it finds a match it sets the boolean isInvalidWord to true, you can make a method and put this in a but I think I might let you play with that a bit.

Honestly I was kind of hesitant to bring it up as I'm sure it will be covered in the upcoming course, as far as the extra credit goes,

a simplier fix that won't have you diving so deep finding your own information (if that is what you wanted, I think I gave you a good start, if you wanted a simpler solution I apologize for taking up your time talking about things that were so far away form the course.)

isInvalidWord = (noun.equalsIngoreCase("dork") || noun.equalsIgnoreCase("jerk") || noun.equalsIngoreCase("butthead"));

That would work as well. If you do truly want to know how deep arrays go (again this definitely moving past the parameters of the course) I reiterate my hint above:

Since you got the code to go through a loop, try to make a method out of that code that loops through the array. See where that takes you playing around with it.

Thank you, sir! I definitely appreciate the words of wisdom. I'll play with it and see about getting it rolling. Cheers!

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

No problem, if after playing with it a bit and have more questions feel free to let me know and I'll see if I can further help.

Sorry if I threw useless information at you.