添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Many Kotlin tutorials I have watched / read have this line of code:

var number = Integer.valueOf(readLine())

And while it clearly worked before, it is now throwing a compiler error while using Android studio and Kotlin version 1.3.50. It indicates a type mismatch where the found is String? and the required is String.

Granted, I understand why this is happening, I get that a user could pass null or empty values in via the console and therefore it needs to have the optional null declaration, but I would like to understand how to fix the compiler error and keep similar code without changing too much.

While I can use both of these lines of code:

var number = Integer.valueOf(readLine()!!)
var number = Integer.valueOf(readLine() as String)

I believe those can throw different exceptions as outlined here

I know I am able to 'fix' this problem by using this code:

var number : String? = readLine();
if(number == null){
    number = "0"
val number2 = Integer.valueOf(number);

But it seems horribly inefficient. Is there a shorter way to do this using native Kotlin code?

Use the Elvis operator to specify a default for when the result is null. readLine()?: “0”. – Tenfour04 Jan 6, 2020 at 23:18

If we simply call toInt() on the result from readLine(), we will get an exception if the value provided isn't an actual Integer. In order to avoid an exception, we can use toIntOrNull() from the Kotlin Standard Library.

val x= readLine()?.toIntOrNull() ?: 0

In this case, we read the line (as a String?) and if it is non-null, call toIntOrNull() on it. If that is non-null, we have our answer. Otherwise, we use 0 as the default.

Even though I am primarily a Swift developer, this is a very similar concept. In Swift it is called a nil-coalescing operator, but apparently in Kotlin it is called the Elvis Operator (uh-huh).

The docs are here So your code would look like this:

var num : String = readLine() ?: "0";

If the value before the Elvis operator ?: is not null, it uses that, otherwise it uses the second default value you provide.

This answer is valid and makes it so I can utilize the existing sample code I mentioned, so +1 for that, but I will mark the other answer as correct just since it also shortens the needed code, too. Thanks for the help! – PGMacDesign Jan 6, 2020 at 23:29

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.