Censoring Words - Using String Equality4:51 with Craig Dennis
In this video, we will block a specific word from being used in our application.
All right, so let's block some words from being allowed to be used. 0:00 As a user of the Internet, I am sure you've seen 0:04 how not nice humanity can be when hiding behind a keyboard. 0:06 Let's do our part in attempting to block this from happening. 0:10 We'll take a few simplistic approaches to the problem. 0:13 But I'm gonna use these approaches to introduce you to a few more commonly used 0:15 tools in your Java toolbox. 0:19 First, let's just block the word dork from being used as our noun. 0:22 We'll start with the same if and 0:26 then exit pattern that we followed blocking out our underaged users. 0:28 We've already seen how methods can be called on objects, like console, system, 0:32 and integer. 0:36 Now, here's something that might strike you as a little strange at first. 0:37 The strings that we've gotten back from our users, they are objects too. 0:40 And just like console, system, and integer, they also have methods. 0:44 Let's pop over the official documentation and 0:49 take a quick peek at what methods are available to strings. 0:51 I'll just do a quick search here for, java String. 0:54 And the fist result here, is from the official Oracle documentation. 0:57 And it's for the Java platform SE, which is Standard Edition 7. 1:01 We're currently running 8 in WorkSpaces, but 1:05 the thing that we're gonna look for has been around forever. 1:08 So let's just go in here. 1:10 And we can scroll down here in this documentation, very long documentation. 1:13 And there's some bunch of ways that you can construct the string. 1:19 Here's the methods that are available. 1:22 Let's scroll down a bit here. 1:24 Again, what we're looking for 1:26 is we're gonna see if noun is actually equal to dork, all right? 1:27 Cuz we wanna try to block that. 1:31 So, is there anything in here about equals? 1:32 Here's something right here, 1:35 equals compares the string to the specified object. 1:36 That sounds perfect. 1:39 And it returns a boolean, which remember, is true or false. 1:40 And that's what we wanna kinda use in our IF statement. 1:42 So let's open that up. 1:45 Let's take a look. 1:45 So this is equals, and it returns a boolean. 1:47 And it compares the string to the specified object that sounds perfect. 1:51 Let's give that a go. 1:54 So we'll go right here after we have the noun. 1:57 And we'll do another if statement. 2:00 So we're gonna say, if, and then we're gonna say the noun dot, 2:02 cuz has methods, equals("dork"). 2:09 Okay, so that's our statement, cuz that's gonna return a boolean. 2:17 And that will be be true if they've typed dork. 2:20 And we'll open up our code block. 2:22 We'll say, write out of the screen, 2:24 That language is not allowed. 2:30 Exiting. 2:34 And we'll exit the program using that System.exit. 2:42 And we'll pass in 0. 2:45 So we expected that. 2:47 And we'll close the parentheses here. 2:48 Okay, so I'm gonna save. 2:50 And I'm gonna run and compile. 2:53 I'm 20. 2:57 I wish. 2:59 My name's Craig. 3:01 And what they wrote was big and dork. 3:03 That language is not allowed. 3:08 Exiting. 3:09 Awesome it worked, we’re blocked. 3:10 When I was looking at the documentation, 3:14 I noticed that there is also a See Also section. 3:15 Let's go and and just check out and ensure that we got the right one. 3:18 The See Also, comparedTo, and ooh, equals ignore case. 3:23 And case there is talking about letter case. 3:27 So an upper case a or lower case a. 3:30 Sounds like exactly what we want. 3:33 Let's see that. 3:34 Compares a string to another string ignoring case considerations, perfect. 3:35 Case strings are considered equal ignoring case if they are the same length. 3:39 Perfect, let's do that, cuz that way if somebody yelled, 3:43 DORK in all uppercase letters, it would get past our censoring code. 3:47 And I don't want that to happen. 3:51 So, let's go ahead and let's switch to that method. 3:51 So we'll say, equalsIgnoreCase. 3:55 Save, and let's go and compile again. 3:58 Again, big. 4:07 And then let's try to break it. 4:08 We'll say DoRk. 4:09 Awesome, great job. 4:13 We stopped the haters. 4:15 Excellent job! 4:17 No one's gonna call me a dork now, but, well, at least not on our program. 4:18 I'm glad you got a chance to get slightly familiar with the documentation. 4:23 It can definitely be a bit verbose, but you shouldn't let that overwhelm you. 4:26 Remember, much like after a few weeks of learning Spanish, 4:30 you shouldn't expect to be able to open the Spanish textbook to Chapter 15 and 4:33 understand all that information. 4:36 But what you should feel good about is that some of those words 4:39 are now starting to make sense, you've learned a lot and you're doing great. 4:42 We'll expand our approach to the next video. 4:46 But first, let's test out those new string methods we just learned. 4:48
You need to sign up for Treehouse in order to download course files.Sign up