Last active
April 14, 2017 05:34
-
-
Save goofmint/45e3fa6dd8d92dedbf5d285ff9feea67 to your computer and use it in GitHub Desktop.
2.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A reference must be explicitly marked as nullable to be able hold a null. | |
* See http://kotlinlang.org/docs/reference/null-safety.html#null-safety | |
*/ | |
package multiplier | |
// Return null if str does not hold a number | |
fun parseInt(str: String): Int? { | |
try { | |
return str.toInt() | |
} catch (e: NumberFormatException) { | |
println("One of the arguments isn't Int") | |
} | |
return null | |
} | |
fun main(args: Array<String>) { | |
if (args.size < 2) { | |
println("No number supplied"); | |
} else { | |
val x = parseInt(args[0]) | |
val y = parseInt(args[1]) | |
// We cannot say 'x * y' now because they may hold nulls | |
if (x != null && y != null) { | |
print(x * y) // Now we can | |
} else { | |
println("One of the arguments is null") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment