Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Java Basics!
You have completed Java Basics!
Preview
In this video, we wrap up the course by introducing a mechanism to continually prompt until a valid response is obtained.
Extra credit
- 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 you 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?
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
In the last video, we talked about
how it was
0:01
frustrating
to just exit the program immediately
0:03
when a user entered a word
that we wanted to block.
0:06
It would be a much better user experience
if we keep asking
0:09
the user to enter a proper word
until they enter an acceptable one.
0:12
This is a common pattern in programming,
usually solved by using a while loop.
0:17
A while loop runs the same block of code
repeatedly while a condition is true.
0:21
Java also has a nice feature
called a do-while loop.
0:26
This lets us run the code once first,
then check the condition afterward.
0:29
Let's use the do-while loop
to keep prompting the user
0:34
until they enter a valid noun.
0:37
Okay, so first, we want to locate
the block of code that we want to repeat.
0:40
We want to keep asking for a noun, right?
0:45
So let's start right above that line.
0:47
So we start with the keyword do,
0:50
and open a curly brace
to begin the loop block.
0:53
I'm going to highlight all of this
noun business and tab it over for clarity.
0:56
Next, let's update the message
that was previously telling the user
1:01
the program is exiting.
1:05
We'll say try again.
1:06
We'll remove the call to system.exit too,
1:11
because we don't want to exit anymore.
1:13
Okay,
1:17
now we need to close that block
with a curly brace.
1:18
So that's all the code that will repeat
if necessary.
1:23
After the closing curly brace,
we add the while statement.
1:27
So we say while and get our parentheses
for our conditional.
1:31
And we want to loop
while the input is one of these words.
1:36
And we already have that typed out
So let's just copy and paste this here.
1:39
Don't forget
1:52
the semicolon at the end of the while line.
1:52
Now, I hope you just had a thought like, wait
a minute, aren't we repeating ourselves?
1:55
Yes, we are. Nice catch!
2:00
And that's a problem. We'll fix that soon.
2:02
What I've also done here
is actually introduce a bug into our code,
2:05
but I want to run it
so we can all experience and see it.
2:09
It's a very common one,
and knowing how and why
2:11
to fix
it will save you time in the future.
2:14
So let's save, compile, and run.
2:17
You'll see that we get this error
saying cannot find symbol,
2:25
pointing to the variable noun.
2:28
But noun is right here.
2:31
What's going on?
2:33
This happens because of variable scope.
2:35
So every time we open a block of code,
like we've done here,
2:38
it creates its own variable scope.
2:41
Variables declared inside a block
2:44
can only be used inside that block.
2:47
Since we declared noun inside the loop,
2:50
the formatted string below
and outside can't see it.
2:52
It's out of scope.
2:56
To fix this, let's declare the variable
before and outside of the loop.
2:58
So we'll say string, noun,
and we don't need to assign it a value,
3:03
it just needs to be declared
in this outer scope.
3:08
Now below,
3:11
where we're assigning its value,
we need to remove this declaration type,
3:12
because we want to be talking
about the same noun variable up here.
3:16
Okay let's try that out now
that both the loop and the printf
3:20
use the same noun variable.
I'll save and run
3:23
our program.
3:27
Okay 33 Travis
3:31
big and I'll try typing dork
3:36
and awesome, it's asking me to try again.
3:39
How nice of it to do so!
3:42
Hmm, jerk?
3:44
Nope, darn.
3:45
Oh, I know. Dude.
3:47
Totally nectar!
3:50
Now about that repeated condition in the
if statement.
3:52
Now what is the best tool for
when you want to store and reuse a value?
3:55
A variable, right?
4:00
We can evaluate
and store the results of our condition
4:01
that is in the if statement
and store the value in a variable.
4:04
As we said earlier,
that is called a Boolean,
4:09
a primitive data
type that stores true or false.
4:12
Let's create a Boolean
4:16
variable called isInvalidWord
4:17
to store the result of our check.
4:20
So let's just move this entire condition.
4:24
Then, in the
4:34
if statement, we can use that Boolean.
4:35
if isInvalidWord
4:41
And let's use it
here in the while as well.
4:44
Oh, but when doing so, I noticed something
else.
4:52
Currently, we are defining a boolean,
but we are again trying to use it
4:55
outside of our scope block.
4:59
So let's declare this
before the loop again so we can use it
5:01
outside.
5:04
Cool! Let's go ahead,
5:14
save and compile,
and make sure things are still working.
5:16
22 Travis
5:26
Big Dork?
Jerk?
5:31
Labrador Retriever?
5:38
Awesome!
5:43
It works perfectly! Congratulations!
5:44
you now have a fully censorship-ready
prototype.
5:46
Awesome job adding loops to your skill set
5:51
and learning how to run code repeatedly
until a condition is met.
5:54
We used a do-while loop to run code
at least once before checking conditions
5:58
and stored condition results in a Boolean
variable to avoid repeating code.
6:03
There are other loops
that we'll cover in a future course.
6:08
We also created a friendly way to sensor
input
6:11
without forcing the users
to restart or lose data.
6:14
After feedback from the
6:18
beta testers,
I'm really happy with where we are.
6:19
I would to congratulate you.
6:22
You've created a program, programmer.
6:24
I encourage you to show your project
to family and friends.
6:28
Try expanding it
by adding more parts of speech
6:31
or experimenting
with other censoring methods.
6:34
I've added some suggestions
in the teacher's notes for more advanced
6:37
solutions. There are
6:41
better ways to solve some problems,
but we haven't covered them yet.
6:42
The main point is,
you created a working application,
6:46
immersed yourself in Java,
and did an excellent job.
6:50
I hope you had fun along the way.
6:53
Thanks for hanging
out. I'll see you next time!
6:55
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up