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 use method " contains" to store a list ?

how can i use method " contains" to store a list ?

1 Answer

Fahad Mutair
Fahad Mutair
10,359 Points

hi Joe Nguyen , if you talking about the extra credit in that video , i solve it in this way

Extra credit

There is a method on string called contains. It checks to see if one string is contained within the other. This seems like a good way to build a master list of words that are not allowed. Why don't see if what they typed is in a long string of bad words. After you get that working, attempt to take case into account. If your list of bad words that you maintain is all lower cased, you can check the lower case version of the input in the bad string. How do you make a string lowercase?

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