Programming in Kotlin: Fundamentals

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

Part 1: Use Data Types & Operations

8. Challenge: Practice If Expressions & Boolean Logic

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See versions
See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 7. Branch with If Expressions & Scopes Next episode: 9. Conclusion

This video was last updated on Aug 9 2022

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

Booleans and if statements and expressions are really powerful, which is why I’ve got an awesome challenge for you to practice them! :]

Challenge :
Check if the user data for registering an account is valid. 

Data is valid if:

1. Properties are not empty.
2. Username has at least 6 characters.
3. Password has at least 10 characters.
4. The email contains a '@' and a ‘.`.

Hint: use the `contains()` function on a String to check if a value is contained in it.
Hint: use the `isEmpty()` function on a String to check if the string is empty.
Example: email.isEmpty()

Use `if` as an expression, to assign a respective error message if any of the cases fail! Then print it out.

The messages are prepared for you in the project.
val email = "filip@mail.com"
val password = "password123"
val username = "filip.babic"
val message = if(email.isEmpty() || password.isEmpty() || username.isEmpty()) {

} else if (username.length < 6) {
  
}
val message = if(email.isEmpty() || password.isEmpty() || username.isEmpty()) {

} else if (username.length < 6) {
  
} else if(password.length < 10) {
  
}
val message = if(email.isEmpty() || password.isEmpty() || username.isEmpty()) {

} else if (username.length < 6) {
  
} else if(password.length < 10) {
  
} else if (!email.contains("@") || !email.contains(".")) {
  
}
val message = if(email.isEmpty() || password.isEmpty() || username.isEmpty()) {

} else if (username.length < 6) {
  
} else if(password.length < 10) {
  
} else if (!email.contains("@") || !email.contains(".")) {
  
} else {
  
}
val message = if (email.isEmpty() || password.isEmpty() || username.isEmpty()) {
  "You must fill in your data!"
} else if (username.length < 6) {
  "Username needs to be at least 6 characters long!"
} else if (password.length < 10) {
  "Password needs to be at least 10 characters long!"
} else if (!email.contains("@") || !email.contains(".")) {
  "Invalid email format."
} else {
  "Successful registration!"
}
println(message)
val email = "filip@mail.com"
val password = "password"
val username = "filip.babic"