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

Censoring words -Java Beta Program

Problem: I just noticed an easy way of bypassing the censorship while testing the beta program.

If the value of the variable noun is "dork" or "jerk" regardless of case it will be rejected. But if the user enters "dork", "_jerk" ( "" implying both a space, multiple spaces, or an actual underscore) the value will get through.

Code: isInvalidWord = (noun.equalsIgnoreCase("dork") || noun.equalsIgnoreCase("Jerk"));

Question: How can you prevent such variations from getting through without being redundant with your code? Such as:

Alt. Code: isInvalidWord = (noun.equalsIgnoreCase("dork") || noun.equalsIgnoreCase("Jerk") || noun.equalsIgnoreCase("_Jerk") || noun.equalsIgnoreCase(" Jerk") || noun.equalsIgnoreCase("_Dork") || noun.equalsIgnoreCase(" Dork"));

Would you use the contains method and store all the possible variations in that? (Coming from a beginner)

Thanks!

1 Answer

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Nice thinking Matthew! Thinking through the edge cases is something you end up doing every time you send an application into the wild, and usually you only think of about half of them, most of the others are found from users reporting errors.

So to answer your question, typically you'd do two things to the input string. First things first, you'd make a copy of it so you can keep what they originally had. Then you'd lowercase it and then do a thing called [trim](http://docs.oracle.com/javase/8/docs/api/java/lang/String.html#trim() which removes the spaces around the word.

Then after you have the value you'd use something like contains, maybe you'd use a thing known as a data structure that is better suited for this use case. Also we really should have tests written so you can add to the edge cases.

Again, great job thinking through the problem space!

Thanks Craig! After reading through the links that makes a lot of sense. I'm going to have to practice writing the code to bring it all together.

PS: As a new student of java your videos are great. I'm learning so much from this track!