Censoring Words - Looping Until the Value Passes7:16 with Craig Dennis
In this video, we wrap up the course by introducing a mechanism to continually prompt until a valid response is obtained.
- This is a very short introduction to loops. Some of your fellow classmates contacted me and asked for more. If you want more too, I did a live workshop called Feeling Loopy with Java.
- 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?
At the end of the last video, we talked about how it was frustrating to do such 0:00 a hard exit when there was a word that was entered that we wanted to censor. 0:04 It's probably, in fact, 0:07 much better if we continue to prompt the user until we get a proper value. 0:08 This is a common pattern in programming. 0:13 And it's usually solved using what is known as a while loop. 0:15 Now what happens is you run the same code block until a certain condition 0:18 is no longer true. 0:23 In other words, while this condition is true, do these things. 0:24 Java has a nice feature called a do while, 0:29 which allows us to first run the code once before any live checking is done. 0:32 Let's use the do while loop to make sure that the user enters a valid word. 0:36 Okay, so the first thing we wanna do is locate the book that we wanna repeat. 0:41 So like we said, we wanna continuously prompt the user until we get a valid now. 0:46 So it looks like here is a good place to start. 0:50 So in order to repeat the code, what we'll do is use the keyword do, and 0:56 then we'll do an open bracket there, 1:00 which that signifies that there's a new code block coming. 1:03 I'm gonna highlight these. 1:07 I'm gonna press the Tab key. 1:08 Great. 1:11 Okay, so now what we want to do is, we want to change the exiting text, 1:12 because we're no longer gonna exit. 1:15 So let's come over in here, and we'll change this to say try again, 1:17 and then we'll get rid of the system exit, because there no where we're gonna exit. 1:22 Okay. 1:29 So next I'm gonna say no, because we're in the do loop code book, 1:30 and then we're going to put in rail. 1:36 All right and so now, we are gonna loop while these things are invalid, so 1:40 we already have a statement for this which is that and I will do, and 1:44 then we'll do this and we'll end in semi colon. 1:50 All right. 1:55 Now I hope that is starting to feel wrong to you, and 1:56 that you just I thought that said wait a second, you said don't repeat yourself. 1:59 Well don't worry, we'll fix that in the minute. 2:03 So in addition to repeating ourselves, I just introduced a bug into this code, but 2:05 I want you to experience it as it's very common and knowing how and 2:09 why to fix it will save you loads of time. 2:13 Okay, so let's go ahead and save this and let's compile it, and 2:16 we'll get an error here. 2:20 OK, so it says error cannot find symbol, and it's pointing here to noun. 2:22 That's interesting cause we very clearly define noun here and set right here, 2:29 set inside of this block instead of this two block here. 2:33 The problem arises, because of something called scope. 2:36 Every time we open up a block of code, like we do right here. 2:40 It opens up a new variable scope and code run outside of the scope. 2:45 So see here's the closing here, and here's the noun that we're using and 2:50 see it's outside of that scope. 2:54 So everything that runs outside of that scope can't see 2:56 the variables declared inside. 2:59 Now the reason for this, and why you might want to use this is a more advanced topic. 3:01 And we'll get into that later in a further course. 3:04 But for now, I'd like to assist you in solving the current problem 3:07 of the outer scope not knowing about the variable. 3:11 And it's actually pretty easy. 3:14 What we want to do is we just want to declare the variable outside. 3:15 So we'll say string.noun. 3:19 And then we want to remove the declaration here, 3:23 because we want it to be talking about the same variable. 3:25 All right? So declare it here, and 3:28 then we'll use it here. 3:30 Okay, so let's try that out. 3:32 I'm gonna save. 3:34 Let's compile and run. 3:37 Great. 3:40 Okay, so let's see, dork, the language is not allowed, I'll try again. 3:44 Jerk, no? 3:49 Awesome. 3:53 Okay so now back to that bad practice we introduced of repeating ourselves. 3:55 So what tool do we use when we want to store a value and reuse it? 4:00 That's right, a variable. 4:04 We can evaluate and store the results of the condition that's in the if statement 4:06 and store that in a variable. 4:10 As we said earlier that's called a boolean value. 4:13 A boolean is a primitive data type and it starts true or false, and 4:16 it uses the lower case convention. 4:19 Let's declare our variable. 4:21 That's boolean is invalid. 4:26 Word equal, I'm wanna go ahead and 4:30 use the same trick that we did before with the parenthesis. 4:32 So I'm gonna copy that. 4:39 Same here. 4:43 See if we can make that look a little bit cleaner. 4:45 That looks pretty good. 4:50 So now we can use the boolean. 4:52 We can say if is invalid word, 4:54 we can use that boolean here that code that we duplicated. 4:57 And we also notice something else. 5:09 We notice we're defining inside of the scope here. 5:11 We're defining a boolean, but when we are using it outside of the code block. 5:14 What do we do? 5:18 We're going to go ahead and do what we just learned and move this boolean, 5:18 put it up here. 5:21 Great. 5:28 Go ahead and save, compile, let's make sure things are still working. 5:30 Great it is excellent. 5:34 I think we have a fully censorship ready prototype. 5:43 Awesome job adding loops to your skillset. 5:47 Now we know how to continuously run our code until a specific condition is met. 5:50 I'm certain that you'll end up using this pattern quite a bit. 5:54 We also learned that we can use a do while loop to make sure that the code runs at 5:58 least once first before looping. 6:01 There are other types of loops that will investigate in future projects, but 6:04 the basic idea is the same for all of them. 6:07 We store the results of a condition in a boolean variable, so that we can reuse it 6:10 and not repeat ourselves in code, and thus keeping things dry. 6:14 We came up with a user friendly way to censor input, 6:18 without causing the users to restart or lose data. 6:21 I'm quite happy with where we ended up after soliciting feedback from 6:24 our beta testers. 6:27 I'd like to congratulate you here. 6:29 We have a working prototype. 6:31 I'd like to encourage you to show it off to your family and your friends. 6:33 You should practice by making the story longer. 6:36 Add different parts of speech and experiment with other ways of censoring. 6:38 Share your findings with the community. 6:42 I've added a couple of suggestions of directions that I'd like you to explore in 6:44 the teacher's notes. 6:47 There are better ways to solve some of the problems that you're likely to run into. 6:49 We just haven't gone over them yet. 6:53 But the point is this, you created a working application, and you immersed 6:54 yourself in the Java language, and you did an awesome job at it. 6:58 I hope you had a fun time doing it. 7:02 I look forward to walking through our next problem introducing you to more and 7:04 more tools and concepts. 7:07 Thanks again for hanging out, and I'll see you next time. 7:09 After the exercise of course, 7:12 you didn't think I was going to let you go without a challenge did you? 7:13
You need to sign up for Treehouse in order to download course files.Sign up