Kotlin example for Beginners – Kotlin Program to Print an Integer (Entered by the User)

Kotlin Program to Print an Integer (Entered by the User)

In this program, you’ll learn to print an integer entered by the user. The integer is stored in a variable and printed to the screen using nextInt() and println() functions respectively.

Example 1: How to Print an Integer entered by an user in Kotlin using Scanner


import java.util.Scanner

fun main(args: Array<String>) {

    // Creates a reader instance which takes
    // input from standard input - keyboard
    val reader = Scanner(System.`in`)
    print("Enter a number: ")

    // nextInt() reads the next integer from the keyboard
    var integer:Int = reader.nextInt()

    // println() prints the following line to the output screen
    println("You entered: $integer")
}

When you run the program, the output will be:

Enter a number: 10
You entered: 10

In this example, an object of Scanner class is created, reader which takes input from the user from keyboard(standard input).

Then, nextInt() function reads the entered integer until it encounters a new line character n (Enter). The integer is then saved in a variable, integer of type Int.

Finally, println() function prints integer to the standard output: computer screen using string templates.


Example 2: How to Print an Integer without using Scanner

fun main(args: Array<String>) {

    print("Enter a number: ")

    // reads line from standard input - keyboard
    // and !! operator ensures the input is not null
    val stringInput = readLine()!!

    // converts the string input to integer
    var integer:Int = stringInput.toInt()

    // println() prints the following line to the output screen
    println("You entered: $integer")
}

When you run the program, the output will be:

Enter a number: 10
You entered: 10

In the above program, we use the function readLine() to read a line of string from the keyboard. Since readLine() can also accept null values, !! operator ensures not-null value of variable stringInput.

Then, the string stored in stringInput is converted to an integer value using the function toInt(), and stored in yet another variable integer.

Finally, integer is printed onto the output screen using println().

 

Python Example for Beginners

Two Machine Learning Fields

There are two sides to machine learning:

  • Practical Machine Learning:This is about querying databases, cleaning data, writing scripts to transform data and gluing algorithm and libraries together and writing custom code to squeeze reliable answers from data to satisfy difficult and ill defined questions. It’s the mess of reality.
  • Theoretical Machine Learning: This is about math and abstraction and idealized scenarios and limits and beauty and informing what is possible. It is a whole lot neater and cleaner and removed from the mess of reality.

Data Science Resources: Data Science Recipes and Applied Machine Learning Recipes

Introduction to Applied Machine Learning & Data Science for Beginners, Business Analysts, Students, Researchers and Freelancers with Python & R Codes @ Western Australian Center for Applied Machine Learning & Data Science (WACAMLDS) !!!

Latest end-to-end Learn by Coding Recipes in Project-Based Learning:

Applied Statistics with R for Beginners and Business Professionals

Data Science and Machine Learning Projects in Python: Tabular Data Analytics

Data Science and Machine Learning Projects in R: Tabular Data Analytics

Python Machine Learning & Data Science Recipes: Learn by Coding

R Machine Learning & Data Science Recipes: Learn by Coding

Comparing Different Machine Learning Algorithms in Python for Classification (FREE)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.