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 will block a specific word from being used in our application.
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
Alright, now let's block
some words from being used in our program.
0:01
As an internet user,
you've probably seen how impolite
0:04
humans can be
when hiding behind a keyboard.
0:08
Let's do our part to prevent that behavior
by blocking some offensive words.
0:11
We'll keep it simple for now,
but I'll also use this as a chance
0:15
to introduce you
to some useful tools in your Java toolbox.
0:18
First, let's block
the word dork from being used as a noun.
0:22
We'll use the same
if and then exit pattern we used earlier
0:26
for the age restriction.
0:29
So we've seen how methods can be called on
0:32
objects console, system, and integer.
0:34
Here's
something you might find interesting.
0:38
The strings we get from
users are objects too.
0:40
And just those other objects,
strings have their own methods.
0:43
Let's take a quick
look at the official Java documentation
0:47
to see what methods
are available for strings.
0:50
I'll search Google for Java string.
0:54
The first result is from Oracle's
0:58
official documentation for Java SE 8.
0:59
That's Standard Edition 8.
1:03
We currently running Java 21 in workspaces
1:05
but the methods we need
have been around for a long time
1:08
Scrolling through the documentation
we see many ways
1:12
to create strings and lots of methods
1:14
What we want is a way to check
if the noun equals dork.
1:18
So we're looking for something with equals
and hey, look right
1:22
here, equals.
1:26
It says
1:32
it compares one string to another
and returns a boolean.
1:32
Remember, that's true or false.
1:36
That sounds perfect for our conditional.
1:38
Let's try it out.
1:40
So let's go right below here
where we get our noun
1:42
from the user,
and let's start another if statement.
1:45
So we'll say if,
1:49
and in our parens
we'll say noun dot equals,
1:51
add our parens for this method, and inside
1:56
we'll put the string dork.
1:59
Then we'll open our code block,
2:03
which will run if this is true,
and let's put a message again.
2:04
Let's write console.printf.
2:08
That language is not allowed
2:12
Exiting
2:17
Then we exit the program
2:22
the same way as before
2:23
And make sure you have your closing curly
brace there.
2:29
So if they're noun equals dork,
let them know it's unacceptable and exit.
2:32
Cool?
2:36
Let's save and test it out.
2:37
I enter age 37, name Travis,
2:45
and what they wrote before was big
2:50
dork.
2:54
That language is not allowed.
2:56
Exiting. Awesome.
2:57
It worked. We blocked the word.
2:59
While browsing the documentation,
I noticed a see also section
3:02
on that equals
method. Let's go check that out.
3:05
So it
3:09
mentions compare to and equals
ignore case.
3:09
The latter sounds useful
3:13
because it ignores letter
case, uppercase and lowercase letters.
3:14
That's exactly what we want.
3:18
If someone types dork in all caps,
it shouldn't bypass our filter.
3:20
Let's update our code to use
equalsIgnoreCase instead.
3:24
Noun dot equalsIgnoreCase
dork.
3:33
Let's save, compile
3:37
and run our code again.
3:39
I'll say 75, Travis,
3:47
Big, and I'll enter
3:52
Dork again, but with mixed up casing.
3:54
Great!
3:57
The program
now blocks these case variations too.
3:58
Excellent job!
4:02
One less troll is allowed to troll.
4:03
I'm glad you got a chance to get
slightly familiar with the documentation.
4:06
It can definitely be a bit verbose,
but don't let that overwhelm you.
4:10
Remember, much after a few weeks
of learning a new language, say Spanish,
4:14
you shouldn't expect to be able to open up
chapter 15
4:19
of your Spanish book
and understand all that information.
4:23
But what you should feel good about is
some of those words are starting
4:26
to make sense.
4:29
You're learning a lot and doing great.
4:31
We'll expand on this
approach in the next video, but first,
4:34
let's do an exercise to practice
those new string methods we just learned.
4:37
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