Kotlin - "If readLine() is null or empty assign default value, else parse readLine()"

kotlin vs java
kotlin tutorial
kotlin android
kotlin ide
kotlin java
kotlin ios
kotlin example
kotlin syntax

I'm a beginner to Kotlin and am loving it so far. I'm curious if Kotlin has a quick and simple solution to checking user input/strings for emptiness. I'd like the funtionality of the following:

"If readLine() is null or empty assign default value, else parse readLine()"

And so far, what I've come up with is this:

var inp = readLine()
val double = if(inp.isNullOrEmpty()) 0.0 else inp.toDouble()

Is this the best it can be? I'd prefer to not store the original user input string if possible. Thanks for the help!

You can use toDoubleOrNull here:

val double: Double = readLine()?.toDoubleOrNull() ?: 0

If readLine() returns null, then the readLine()?.toDoubleOrNull() expression will also return null, and you'll fall back on the default value.

If readLine() returns a non-null value, toDoubleOrNull will attempt to parse it, and if it fails (for example, for an empty string), it will return null, making you fall back to the default once again.

Kotlin and Android, Kotlin for Android Developers: Learn Kotlin the Easy Way While Developing an Android App Kotlin was announced as an official Android development language at Google I/O 2017. It became the third language fully supported for Android, in addition to Java and C++. Adoption. In 2018, Kotlin was the fastest growing language on GitHub with 2.6 times more developers compared to 2017.

Learn the Kotlin programming language, Programming Kotlin: Create Elegant, Expressive, and Performant JVM and Android Applications Kotlin is a programming language is widely used by Android developers everywhere. This topic serves as a Kotlin crash-course to get you up and running quickly.

How about this solution:

System.console().readLine()?.ifBlank { null } ?: "default string value"

and for password (this doesn't work inside IntelliJ):

String(System.console().readPassword()).ifBlank { "password" }

Kotlin (programming language), Kotlin is a free and open source project under the Apache 2.0 license. The code for the project is developed openly on GitHub primarily by the team employed at  Kotlin is a programming language introduced by JetBrains, the official designer of the most intelligent Java IDE, named Intellij IDEA. This is a strongly statically typed language that runs on JVM. In 2017, Google announced Kotlin is an official language for android development.

What is Kotlin? The Java alternative explained, Variable declaration. Kotlin uses two different keywords to declare variables: val and var . Use val for a variable whose value  Kotlin is a modern statically typed programming language that will boost your productivity and increase your developer happiness.

Kotlin ✓, Kotlin is a general purpose, free, open source, statically typed “pragmatic” programming language initially designed for the JVM (Java Virtual Machine) and Android that combines object-oriented and functional programming features. In Kotlin, the interface works exactly similar to Java 8, which means they can contain method implementation as well as abstract methods declaration. An interface can be implemented by a class in order to use its defined functionality. We have already introduced an example with an interface in Chapter 6 - section “anonymous inner class”.

JetBrains/kotlin: The Kotlin Programming Language, Check out the latest insights about Kotlin in a dedicated section of the State of Developer Ecosystem 2020, a survey and report by JetBrains. #DevEcosystem20 jb.gg/devecosystem20-kotl… kotlin@kotlin.cz +420 603 448 886 user Přihlásit. cart Košík je prázdn ý. menu Menu Kategorie snímačů

Comments
  • Ah this is perfect. Thank you!