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 String Equality

Mohammed Awwad
Mohammed Awwad
393 Points

so what if i want to block more than one word do I have retype the same line over and over or there is an easier way ?

so what if i want to block more than one word do I have retype the same line over and over or there is an easier way ?

4 Answers

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

Hey Mohammed,

Good question, you can block them by adding additional logical operators in your code such as

if (answer.equalsIgnoreCase("blockedword") || answer.equalsIgnoreCase("Blockedword2") || answer.equalsIgnoreCase("blockedword3") {
     throw new IllegalArgumentException("Sorry, that word is blocked");
}

Adding as many words as you want with as many additional || operators in your if statement as you want.

Though it's moving a bit out of the realm of what was taught in the course, you could also create an array, which is an object that we will be learning about in the next course, that contains your restricted words, and write code to see if that word is contained in the array or not, but this will be covered a bit more in detail once we learn about arrays.

Thanks, let me know if this helps!

Mohammed Awwad
Mohammed Awwad
393 Points

it didn't work and it have a mistake in the code wish is answer.equalsIgnoreCase("Blockedword2"
you forget to add ) at he end of blockword2 *sorry don't mean to be rude

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

I saw that and I edited it, but thanks for catching it. Can you post your code and I can give it a look?

That above was really just a snippet of what an in general statement would look like.

Thanks.

Mohammed Awwad
Mohammed Awwad
393 Points

sorry i didn't save it because i didn't want to make an error in my work space but hear is the regional code

String noun = console.readLine("Enter a noun: "); if (noun.equalsIgnoreCase("dork")) { console.printf("That language is not allowed. Exting. \n\n"); System.exit(0); }

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

I opened up the old work space and that logic should work, here's what I have and I added a few just to make sure that it worked, and it complied well enough.

 boolean isInvalidWord;
      do {
      noun = console.readLine("Enter a noun:  ");
         isInvalidWord =(noun.equalsIgnoreCase("dork") || 
                                noun.equalsIgnoreCase("jerk") || noun.equalsIgnoreCase("idiot"));
        if (isInvalidWord) {
          console.printf("That language is not allowed. Try again. \n\n");
          }
      } while(noun.equalsIgnoreCase("dork") || noun.equalsIgnoreCase("jerk") || noun.equalsIgnoreCase("idiot"));

Basically it is just looping through a list I created with the restricted words, and if it catches it then it prints out that the language isn't acceptable and continues to prompt for guesses. The above mentioned method to add on words to the list should work, maybe it was a compile error.

It's really hard to tell.

Mohammed Awwad
Mohammed Awwad
393 Points

it worked thanks alto for your concern :)