1 00:00:00,460 --> 00:00:04,160 At the end of the last video, we talked about how it was frustrating to do such 2 00:00:04,160 --> 00:00:07,810 a hard exit when there was a word that was entered that we wanted to censor. 3 00:00:07,810 --> 00:00:08,960 It's probably, in fact, 4 00:00:08,960 --> 00:00:13,340 much better if we continue to prompt the user until we get a proper value. 5 00:00:13,340 --> 00:00:15,160 This is a common pattern in programming. 6 00:00:15,160 --> 00:00:18,990 And it's usually solved using what is known as a while loop. 7 00:00:18,990 --> 00:00:23,070 Now what happens is you run the same code block until a certain condition 8 00:00:23,070 --> 00:00:24,650 is no longer true. 9 00:00:24,650 --> 00:00:28,270 In other words, while this condition is true, do these things. 10 00:00:29,360 --> 00:00:32,330 Java has a nice feature called a do while, 11 00:00:32,330 --> 00:00:35,930 which allows us to first run the code once before any live checking is done. 12 00:00:36,930 --> 00:00:39,990 Let's use the do while loop to make sure that the user enters a valid word. 13 00:00:41,530 --> 00:00:46,470 Okay, so the first thing we wanna do is locate the block that we wanna repeat. 14 00:00:46,470 --> 00:00:50,610 So like we said, we wanna continuously prompt the user until we get a valid noun. 15 00:00:50,610 --> 00:00:54,105 So it looks like here is a good place to start. 16 00:00:56,269 --> 00:01:00,844 So in order to repeat the code, what we'll do is use the keyword do, and 17 00:01:00,844 --> 00:01:03,285 then we'll do an open bracket there, 18 00:01:03,285 --> 00:01:07,670 which that signifies that there's a new code block coming. 19 00:01:07,670 --> 00:01:08,530 I'm gonna highlight these. 20 00:01:08,530 --> 00:01:09,600 I'm gonna press the Tab key. 21 00:01:11,590 --> 00:01:12,160 Great. 22 00:01:12,160 --> 00:01:15,327 Okay, so now what we want to do is, we want to change the exiting text, 23 00:01:15,327 --> 00:01:17,064 because we're no longer gonna exit. 24 00:01:17,064 --> 00:01:22,486 So let's come over in here, and we'll change this to say try again, 25 00:01:22,486 --> 00:01:29,238 and then we'll get rid of the System.exit, because there's nowhere we're gonna exit. 26 00:01:29,238 --> 00:01:30,511 Okay. 27 00:01:30,511 --> 00:01:36,000 So next what I'm gonna say now, because we're in the do loop code book, 28 00:01:36,000 --> 00:01:39,050 and then we're going to put in while. 29 00:01:40,220 --> 00:01:44,600 All right and so now, we are gonna loop while these things are invalid, so 30 00:01:44,600 --> 00:01:50,570 we already have a statement for this which is that and I will do, and 31 00:01:50,570 --> 00:01:55,850 then we'll do this and we'll end in semi colon. 32 00:01:55,850 --> 00:01:56,460 All right. 33 00:01:56,460 --> 00:01:59,410 Now I hope that is starting to feel wrong to you, and 34 00:01:59,410 --> 00:02:03,730 that you just I thought that said wait a second, you said don't repeat yourself. 35 00:02:03,730 --> 00:02:05,800 Well don't worry, we'll fix that in the minute. 36 00:02:05,800 --> 00:02:09,800 So in addition to repeating ourselves, I just introduced a bug into this code, but 37 00:02:09,800 --> 00:02:13,320 I want you to experience it as it's very common and knowing how and 38 00:02:13,320 --> 00:02:15,320 why to fix it will save you loads of time. 39 00:02:16,585 --> 00:02:20,110 Okay, so let's go ahead and save this and let's compile it, and 40 00:02:20,110 --> 00:02:21,280 we'll get an error here. 41 00:02:22,580 --> 00:02:28,640 OK, so it says error cannot find symbol, and it's pointing here to noun. 42 00:02:29,708 --> 00:02:33,090 That's interesting cause we very clearly define noun here and set right here, 43 00:02:33,090 --> 00:02:36,350 set inside of this block instead of this two block here. 44 00:02:36,350 --> 00:02:38,630 The problem arises, because of something called scope. 45 00:02:40,460 --> 00:02:43,110 Every time we open up a block of code, like we do right here. 46 00:02:45,340 --> 00:02:49,730 It opens up a new variable scope and code run outside of the scope. 47 00:02:50,760 --> 00:02:54,610 So see here's the closing here, and here's the noun that we're using and 48 00:02:54,610 --> 00:02:56,590 see it's outside of that scope. 49 00:02:56,590 --> 00:02:59,200 So everything that runs outside of that scope can't see 50 00:02:59,200 --> 00:03:01,470 the variables declared inside. 51 00:03:01,470 --> 00:03:04,920 Now the reason for this, and why you might want to use this is a more advanced topic. 52 00:03:04,920 --> 00:03:07,610 And we'll get into that later in a further course. 53 00:03:07,610 --> 00:03:11,330 But for now, I'd like to assist you in solving the current problem 54 00:03:11,330 --> 00:03:13,220 of the outer scope not knowing about the variable. 55 00:03:14,390 --> 00:03:15,510 And it's actually pretty easy. 56 00:03:15,510 --> 00:03:19,957 What we want to do is we just want to declare the variable outside. 57 00:03:19,957 --> 00:03:23,243 So we'll say String noun. 58 00:03:23,243 --> 00:03:25,467 And then we want to remove the declaration here, 59 00:03:25,467 --> 00:03:28,079 because we want it to be talking about the same variable. 60 00:03:28,079 --> 00:03:30,340 All right? So declare it here, and 61 00:03:30,340 --> 00:03:32,430 then we'll use it here. 62 00:03:32,430 --> 00:03:34,940 Okay, so let's try that out. 63 00:03:34,940 --> 00:03:35,610 I'm gonna save. 64 00:03:37,410 --> 00:03:40,511 Let's compile and run. 65 00:03:40,511 --> 00:03:44,078 Great. 66 00:03:44,078 --> 00:03:49,057 Okay, so let's see, dork, the language is not allowed, I'll try again. 67 00:03:49,057 --> 00:03:50,787 Jerk, no? 68 00:03:53,783 --> 00:03:55,759 Awesome. 69 00:03:55,759 --> 00:04:00,710 Okay so now back to that bad practice we introduced of repeating ourselves. 70 00:04:00,710 --> 00:04:03,540 So what tool do we use when we want to store a value and reuse it? 71 00:04:04,690 --> 00:04:06,690 That's right, a variable. 72 00:04:06,690 --> 00:04:10,750 We can evaluate and store the results of the condition that's in the if statement 73 00:04:10,750 --> 00:04:12,190 and store that in a variable. 74 00:04:13,190 --> 00:04:14,960 As we said earlier that's called a boolean value. 75 00:04:16,120 --> 00:04:19,840 A boolean is a primitive data type and it starts true or false, and 76 00:04:19,840 --> 00:04:21,790 it uses the lower case convention. 77 00:04:21,790 --> 00:04:22,985 Let's declare our variable. 78 00:04:26,458 --> 00:04:30,449 That's boolean isInvalidWord 79 00:04:30,449 --> 00:04:32,592 equal, I'm wanna go ahead and 80 00:04:32,592 --> 00:04:36,173 use the same trick that we did before with the parenthesis. 81 00:04:39,984 --> 00:04:43,319 So I'm gonna copy that. 82 00:04:43,319 --> 00:04:43,890 Same here. 83 00:04:45,580 --> 00:04:47,531 See if we can make that look a little bit cleaner. 84 00:04:50,909 --> 00:04:52,604 That looks pretty good. 85 00:04:52,604 --> 00:04:54,292 So now we can use the boolean. 86 00:04:54,292 --> 00:04:57,158 We can say if is invalid word, 87 00:04:57,158 --> 00:05:02,787 we can use that boolean here that code that we duplicated. 88 00:05:09,983 --> 00:05:11,770 And we also notice something else. 89 00:05:11,770 --> 00:05:14,110 We notice we're defining inside of the scope here. 90 00:05:14,110 --> 00:05:18,010 We're defining a boolean, but when we are using it outside of the code block. 91 00:05:18,010 --> 00:05:18,510 What do we do? 92 00:05:18,510 --> 00:05:21,840 We're going to go ahead and do what we just learned and move this boolean, 93 00:05:21,840 --> 00:05:23,560 put it up here. 94 00:05:28,580 --> 00:05:29,080 Great. 95 00:05:30,110 --> 00:05:34,758 Go ahead and save, compile, let's make sure things are still working. 96 00:05:34,758 --> 00:05:43,870 Great it is excellent. 97 00:05:43,870 --> 00:05:46,860 I think we have a fully censorship ready prototype. 98 00:05:47,930 --> 00:05:50,530 Awesome job adding loops to your skillset. 99 00:05:50,530 --> 00:05:54,850 Now we know how to continuously run our code until a specific condition is met. 100 00:05:54,850 --> 00:05:58,200 I'm certain that you'll end up using this pattern quite a bit. 101 00:05:58,200 --> 00:06:01,880 We also learned that we can use a do while loop to make sure that the code runs at 102 00:06:01,880 --> 00:06:04,810 least once first before looping. 103 00:06:04,810 --> 00:06:07,710 There are other types of loops that will investigate in future projects, but 104 00:06:07,710 --> 00:06:09,430 the basic idea is the same for all of them. 105 00:06:10,620 --> 00:06:14,490 We store the results of a condition in a boolean variable, so that we can reuse it 106 00:06:14,490 --> 00:06:18,400 and not repeat ourselves in code, and thus keeping things dry. 107 00:06:18,400 --> 00:06:21,280 We came up with a user friendly way to censor input, 108 00:06:21,280 --> 00:06:24,670 without causing the users to restart or lose data. 109 00:06:24,670 --> 00:06:27,680 I'm quite happy with where we ended up after soliciting feedback from 110 00:06:27,680 --> 00:06:29,270 our beta testers. 111 00:06:29,270 --> 00:06:31,250 I'd like to congratulate you here. 112 00:06:31,250 --> 00:06:33,700 We have a working prototype. 113 00:06:33,700 --> 00:06:36,710 I'd like to encourage you to show it off to your family and your friends. 114 00:06:36,710 --> 00:06:38,660 You should practice by making the story longer. 115 00:06:38,660 --> 00:06:42,570 Add different parts of speech and experiment with other ways of censoring. 116 00:06:42,570 --> 00:06:44,720 Share your findings with the community. 117 00:06:44,720 --> 00:06:47,980 I've added a couple of suggestions of directions that I'd like you to explore in 118 00:06:47,980 --> 00:06:49,490 the teacher's notes. 119 00:06:49,490 --> 00:06:53,060 There are better ways to solve some of the problems that you're likely to run into. 120 00:06:53,060 --> 00:06:54,830 We just haven't gone over them yet. 121 00:06:54,830 --> 00:06:58,780 But the point is this, you created a working application, and you immersed 122 00:06:58,780 --> 00:07:02,120 yourself in the Java language, and you did an awesome job at it. 123 00:07:02,120 --> 00:07:03,040 I hope you had a fun time doing it. 124 00:07:04,090 --> 00:07:07,610 I look forward to walking through our next problem introducing you to more and 125 00:07:07,610 --> 00:07:09,380 more tools and concepts. 126 00:07:09,380 --> 00:07:11,130 Thanks again for hanging out, and I'll see you next time. 127 00:07:12,200 --> 00:07:13,425 After the exercise of course, 128 00:07:13,425 --> 00:07:16,420 you didn't think I was going to let you go without a challenge did you?