Lesson 5: Keyboard Data Entry on the Console in Kotlin language

Lesson 5: Keyboard Data Entry on the Console in Kotlin language

Hello viewer, You can now have our Tutorial Lessons in your android mobile device and read it offline.
Download kotlin Programming APP on PlayStore
Download Website SEO Lessons APP on PlayStore

Keyboard Data Entry on the Console in Kotlin. When we use the Console to display information on screen we use the print and println functions. If we need to enter data by keyboard we can use the readLine function.

Topic: Keyboard Data Entry on the Console in Kotlin language

Problem 1

Load two integers per keyboard and print your sum and your product.

Project5 – Main.kt

fun main (argument: Array <String>) {
//coding180.com
    print ("Enter first value:")
    val value1 = readLine () !!. toInt ()
    print ("Enter second value:")
    val value2 = readLine () !!. toInt ()
    val sum = value1 + value2
    println ("The sum of $value1 and $value2 is $sum")
    val product = value1 * value2
            println ("The product of $value1 and $value2 is $product")
}

For input of data by keyboard we have a function called readLine. This function returns a String with the characters written by the operator until you press the “Input” key. Then calling the “toInt” method of the String class converts the data entered by keyboard into an Int and saved to value1.

Topic: Keyboard Data Entry on the Console in Kotlin language

The problem occurs when the operator presses the “Input” key without loading data, in that case returns null.

We saw that one of the premises of Kotlin is to be concise, the second premise is that it is safe, then when a function returns null we can not call the methods that have that object:

 value1 = readLine (). toInt()

The previous line does not compile since if readLine returns a String it is correct to call the toInt method to convert it to integer, but readLine can return null and in that case we can not call toInt.

The first way to solve this is to tell the Kotlin compiler to trust that the readLine function always returns a String, we do this by adding the !! In the call:

     value1 = readLine () !!. toInt()

It is not the best way to validate that a data entry by keyboard is mandatory, but in the next concepts we will see how to improve the input of data by keyboard to make it safer and prevent errors from being generated when the operator presses the key “Input “Without entering data.

Topic: Keyboard Data Entry on the Console in Kotlin language

Continuing with the problem after loading the two integers per key we proceed to add them, multiply them and show the results:

     val sum = value1 + value2
     println ("The sum of $value1 and $value2 is $sum")
     val product = value1 * value2
     println ("The product of $value1 and $value2 is $product")

Problem 2

Load the side of a square, display the perimeter of the square (The perimeter of a square is calculated by multiplying the value of the side by four)

Project6 – coding180project.kt

fun main (parameter: Array<String>) {
    print ("Enter the measure of the side of the square:")
    val side = readLine () !!. toInt()
    val perimeter = side * 4
    println ("The perimeter of the square is $perimeter")
}

The variable side by inference is defined as type Int:

     val side = readLine () !!. toInt ()

We remember that in an extensive form we can write the previous code with the following syntax:

     val side: Int
     side = readLine () !!. toInt ()

In Kolin remember that what is wanted that the code is as concise as possible.

Then we calculate the perimeter and we show it by the Console:

     val perimeter = side * 4
     println ("The perimeter of the square is $perimeter")

Problem 3

A program must be developed that asks for the entry of the price of an item and the amount that the customer carries. Show what the buyer must pay.

Topic: Keyboard Data Entry on the Console in Kotlin language

Project7 – Main.kt

fun main (parameter: Array <String>) {
    print ("Enter the price of the product:")
    val price = readLine () !!. toDouble ()
    print ("Enter the number of products:")
    val quantity = readLine () !!. toInt ()
    val total  = price * quantity
    println ("The total to pay is $total")
}

We load by keyboard a value of type Double and by inference the price variable is defined with this type:

     print ("Enter the price of the product:")
     val price = readLine() !!. toDouble()

Then we load the quantity of products to carry, the data that must enter the operator is an integer:

     print ("Enter the number of products:")
     val quantity = readLine () !!.toInt ()

Finally we multiply the Double variable and the variable Int resulting in another Double value:

     total val = price * quantity
     println ("The total to pay is $total")

Problems proposed

  • Write a program in which you enter four integers, calculate and report the sum of the first two and the product of the third and fourth.
  • Perform a program that reads four integer values by keyboard and reports its sum and average.

Topic: Keyboard Data Entry on the Console in Kotlin language

Solution

// Project8

About The Author

Related posts

Leave a Reply