Programming in Kotlin: Fundamentals

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

Part 1: Use Data Types & Operations

08. Challenge: Practice If Expressions & Boolean Logic

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: 07. Branch with If Expressions & Scopes Next episode: 09. Conclusion

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.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

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"