![]() ![]() In this Kotlin Tutorial, we learned how to read an integer from console input using readLine() function and String.toInt() method. ![]() When we entered an invalid value for an integer, parsing failed and NumberFormatException is thrown. We then print the entered value back to console.Įxception in thread "main" : For input string: "asdf"Īt java.base/(NumberFormatException.java:65)Īt java.base/(Integer.java:652)Īt java.base/(Integer.java:770) In the following program, we read an integer number to n from user input. In the Java code below, nullis added as an item to the list. Please note that we have used safe call ?. For example, if Kotlin calls Java code that returns an ArrayList, the inferred type in Kotlin will be ArrayList, which means the collection can be nullable, and the items as well.Once we got the string value, we convert it to an integer value using String.toInt() method, as shown in the following. Please note that the function can also return a null value. The syntax to call readLine() function is readLine() ![]()
0 Comments
Leave a Reply. |