1 00:00:00,310 --> 00:00:03,290 All right, so let's block some words from being allowed to be used. 2 00:00:04,320 --> 00:00:06,880 As a user of the Internet, I am sure you've seen 3 00:00:06,880 --> 00:00:10,120 how not nice humanity can be when hiding behind a keyboard. 4 00:00:10,120 --> 00:00:13,285 Let's do our part in attempting to block this from happening. 5 00:00:13,285 --> 00:00:15,728 We'll take a few simplistic approaches to the problem. 6 00:00:15,728 --> 00:00:19,750 But I'm gonna use these approaches to introduce you to a few more commonly used 7 00:00:19,750 --> 00:00:21,060 tools in your Java toolbox. 8 00:00:22,120 --> 00:00:25,290 First, let's just block the word dork from being used as our noun. 9 00:00:26,385 --> 00:00:28,130 We'll start with the same if and 10 00:00:28,130 --> 00:00:30,690 then exit pattern that we followed blocking out our underaged users. 11 00:00:32,160 --> 00:00:36,717 We've already seen how methods can be called on objects, like console, system, 12 00:00:36,717 --> 00:00:37,506 and integer. 13 00:00:37,506 --> 00:00:40,630 Now, here's something that might strike you as a little strange at first. 14 00:00:40,630 --> 00:00:44,730 The strings that we've gotten back from our users, they are objects too. 15 00:00:44,730 --> 00:00:48,050 And just like console, system, and integer, they also have methods. 16 00:00:49,110 --> 00:00:51,160 Let's pop over the official documentation and 17 00:00:51,160 --> 00:00:54,130 take a quick peek at what methods are available to strings. 18 00:00:54,130 --> 00:00:57,840 I'll just do a quick search here for, java String. 19 00:00:57,840 --> 00:01:01,025 And the fist result here, is from the official Oracle documentation. 20 00:01:01,025 --> 00:01:05,860 And it's for the Java platform SE, which is Standard Edition 7. 21 00:01:05,860 --> 00:01:08,690 We're currently running 8 in WorkSpaces, but 22 00:01:08,690 --> 00:01:10,760 the thing that we're gonna look for has been around forever. 23 00:01:10,760 --> 00:01:13,470 So let's just go in here. 24 00:01:13,470 --> 00:01:19,034 And we can scroll down here in this documentation, very long documentation. 25 00:01:19,034 --> 00:01:22,790 And there's some bunch of ways that you can construct the string. 26 00:01:22,790 --> 00:01:24,505 Here's the methods that are available. 27 00:01:24,505 --> 00:01:26,480 Let's scroll down a bit here. 28 00:01:26,480 --> 00:01:27,440 Again, what we're looking for 29 00:01:27,440 --> 00:01:31,410 is we're gonna see if noun is actually equal to dork, all right? 30 00:01:31,410 --> 00:01:32,710 Cuz we wanna try to block that. 31 00:01:32,710 --> 00:01:35,470 So, is there anything in here about equals? 32 00:01:35,470 --> 00:01:36,833 Here's something right here, 33 00:01:36,833 --> 00:01:39,110 equals compares the string to the specified object. 34 00:01:39,110 --> 00:01:40,040 That sounds perfect. 35 00:01:40,040 --> 00:01:42,930 And it returns a boolean, which remember, is true or false. 36 00:01:42,930 --> 00:01:45,030 And that's what we wanna kinda use in our IF statement. 37 00:01:45,030 --> 00:01:45,990 So let's open that up. 38 00:01:45,990 --> 00:01:46,490 Let's take a look. 39 00:01:47,940 --> 00:01:51,490 So this is equals, and it returns a boolean. 40 00:01:51,490 --> 00:01:54,990 And it compares the string to the specified object that sounds perfect. 41 00:01:54,990 --> 00:01:55,690 Let's give that a go. 42 00:01:57,520 --> 00:02:00,872 So we'll go right here after we have the noun. 43 00:02:00,872 --> 00:02:02,413 And we'll do another if statement. 44 00:02:02,413 --> 00:02:09,514 So we're gonna say, if, and then we're gonna say the noun dot, 45 00:02:09,514 --> 00:02:14,052 cuz has methods, equals("dork"). 46 00:02:17,660 --> 00:02:20,388 Okay, so that's our statement, cuz that's gonna return a boolean. 47 00:02:20,388 --> 00:02:22,492 And that will be be true if they've typed dork. 48 00:02:22,492 --> 00:02:24,927 And we'll open up our code block. 49 00:02:24,927 --> 00:02:30,317 We'll say, write out of the screen, 50 00:02:30,317 --> 00:02:34,702 That language is not allowed. 51 00:02:34,702 --> 00:02:35,962 Exiting. 52 00:02:42,290 --> 00:02:45,579 And we'll exit the program using that System.exit. 53 00:02:45,579 --> 00:02:47,707 And we'll pass in 0. 54 00:02:47,707 --> 00:02:48,583 So we expected that. 55 00:02:48,583 --> 00:02:50,991 And we'll close the parentheses here. 56 00:02:50,991 --> 00:02:53,184 Okay, so I'm gonna save. 57 00:02:53,184 --> 00:02:57,612 And I'm gonna run and compile. 58 00:02:57,612 --> 00:02:59,791 I'm 20. 59 00:02:59,791 --> 00:03:01,141 I wish. 60 00:03:01,141 --> 00:03:01,770 My name's Craig. 61 00:03:03,720 --> 00:03:07,080 And what they wrote was big and dork. 62 00:03:08,320 --> 00:03:09,670 That language is not allowed. 63 00:03:09,670 --> 00:03:10,800 Exiting. 64 00:03:10,800 --> 00:03:12,580 Awesome it worked, we’re blocked. 65 00:03:14,170 --> 00:03:15,620 When I was looking at the documentation, 66 00:03:15,620 --> 00:03:18,160 I noticed that there is also a See Also section. 67 00:03:18,160 --> 00:03:20,710 Let's go and and just check out and ensure that we got the right one. 68 00:03:23,570 --> 00:03:27,880 The See Also, comparedTo, and ooh, equals ignore case. 69 00:03:27,880 --> 00:03:30,510 And case there is talking about letter case. 70 00:03:30,510 --> 00:03:33,480 So an upper case a or lower case a. 71 00:03:33,480 --> 00:03:34,720 Sounds like exactly what we want. 72 00:03:34,720 --> 00:03:35,583 Let's see that. 73 00:03:35,583 --> 00:03:39,730 Compares a string to another string ignoring case considerations, perfect. 74 00:03:39,730 --> 00:03:42,640 Case strings are considered equal ignoring case if they are the same length. 75 00:03:43,750 --> 00:03:47,069 Perfect, let's do that, cuz that way if somebody yelled, 76 00:03:47,069 --> 00:03:51,010 DORK in all uppercase letters, it would get past our censoring code. 77 00:03:51,010 --> 00:03:51,990 And I don't want that to happen. 78 00:03:51,990 --> 00:03:55,320 So, let's go ahead and let's switch to that method. 79 00:03:55,320 --> 00:03:58,547 So we'll say, equalsIgnoreCase. 80 00:03:58,547 --> 00:04:01,079 Save, and let's go and compile again. 81 00:04:07,324 --> 00:04:08,375 Again, big. 82 00:04:08,375 --> 00:04:09,928 And then let's try to break it. 83 00:04:09,928 --> 00:04:13,050 We'll say DoRk. 84 00:04:13,050 --> 00:04:15,420 Awesome, great job. 85 00:04:15,420 --> 00:04:16,390 We stopped the haters. 86 00:04:17,660 --> 00:04:18,498 Excellent job! 87 00:04:18,498 --> 00:04:22,100 No one's gonna call me a dork now, but, well, at least not on our program. 88 00:04:23,100 --> 00:04:26,605 I'm glad you got a chance to get slightly familiar with the documentation. 89 00:04:26,605 --> 00:04:30,183 It can definitely be a bit verbose, but you shouldn't let that overwhelm you. 90 00:04:30,183 --> 00:04:33,124 Remember, much like after a few weeks of learning Spanish, 91 00:04:33,124 --> 00:04:36,989 you shouldn't expect to be able to open the Spanish textbook to Chapter 15 and 92 00:04:36,989 --> 00:04:39,345 understand all that information. 93 00:04:39,345 --> 00:04:42,005 But what you should feel good about is that some of those words 94 00:04:42,005 --> 00:04:46,480 are now starting to make sense, you've learned a lot and you're doing great. 95 00:04:46,480 --> 00:04:48,680 We'll expand our approach to the next video. 96 00:04:48,680 --> 00:04:51,410 But first, let's test out those new string methods we just learned.