Programming in Kotlin: Fundamentals

Aug 9 2022 · Kotlin 1.6, Android 12, IntelliJ IDEA CE 2022.1.3

Part 3: Functions & Nullability

21. Challenge: Use Nullables

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 20. Create & Consume Nullables Next episode: 22. Write Custom Functions

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 21. Challenge: Use Nullables

To get a hang of nullable values, and how to work around them using checks and smart casting, I’ve prepared a challenge for you.

In this challenge, you have to declare a nullable String? named password, and assign a value to it. Then you have to check how secure the password is, according to the rules and levels included in the starter project.

Finally you have assign a security level message to another constant named message, and print it out.

That’s it! Now pause the video, and solve the challenges using the knowledge you’ve gained so far! :]

Then once you’re done, play the video, to compare your solutions with mine. Good luck! :]

Challenge:
Declare a variable of type String? called `password` and assign a value to it.

Using an if expression, check the level of password strength, 
and assign an appropriate message to another constant named `message`. 

Then print out the message.

Levels are designed as follows:

0 characters or `null` -> “Ehm, you need a password to keep safe!”
1-5 characters -> “Weak password! Try adding a few more symbols to it!”
6-10 characters -> “Medium-strength password.”
11-15 characters -> “No one is getting through this!”
15+ characters -> "Ironclad

How did this one go for you? Hopefully you did well.

Alright, let’s solve the challenge. Start by creating a constant named password which is a nullable String. Then put in a value of your choice.

val password: String? = "12345"

This is not really a secure password, and you’re going to prove that with some checks!

Now, you need to check at which level of security the password is, then create an appropriate message, depending on the level. You can do so by following the rules for levels in the project. Go ahead and declare a message constant and assign it to an if statement like so:

val message = if (password == null || password.isEmpty()) {
  "Ehm, you need a password to keep safe!"
}

To start off, you’re checking the most basic cases: the password being null, or empty.

Notice how the password is smart casted to a non-nullable type after the very first null check. You don’t have to worry about it anymore, after the first case. It’s time to add the rest of the cases. I’ll go ahead and paste them in:

...
 else if (password.length in 1..5) {
  "Weak password! Try adding a few more symbols to it!"
} else if (password.length in 6..10) {
  "Medium-strength password."
} else if (password.length in 11..15) {
  "No one is getting through this!"
} else {
  "Ironclad"
}

println(message)

These are all the rules or levels in the project, so you’ve made sure that each level is covered!

Now run the project, and you should get an appropriate message!

Another cool thing you can do is use the Elvis operator to default the password length to zero if the password is null. Then check the passwordLength constant in the if statement and not the password itself. Let’s see how to do this. Update your code to the following:

val password: String? = "12345"
val passwordLength = password?.length ?: 0

val message = if (passwordLength == 0) {
  "Ehm, you need a password to keep safe!"
} else if (passwordLength in 1..5) {
  "Weak password! Try adding a few more symbols to it!"
} else if (passwordLength in 6..10) {
  "Medium-strength password."
} else if (passwordLength in 11..15) {
  "No one is getting through this!"
} else {
  "Ironclad"
}

Run the project once more, to make sure it works correctly.

Cool!!! Everything works fine.

Now change the value of the password to null. Then run your project once again.

As you can see, the appropriate message is displayed.

Do note that you can also use a when statement to solve this challenge and you can try that out if you’re feeling a bit adventurous.

See you in the next episode.