Kotlin program to input a string

Kotlin | Input String: Here, we are going to learn how to input/read and print a string in Kotlin programming language? Submitted by IncludeHelp, on April 16, 2020

How to input a string in Kotlin?

To input a string in Kotlin, we use the "readLine()" method.

Kotlin program to input a string

In the below program, we will input a string from the user and print it on the screen.

package com.includehelp.basic

// Main Method Entry Point of Program
fun main(args: Array<String>) {
    
    // Input name
    println("Enter Your Name: ")
    val name = readLine()
    
    // Input Address
    println("Enter Your Address: ")
    val add = readLine()
    
    // Input Country Name
    println("Enter Your Country Name : ")   
    val country = readLine()
    
    // Printing input values
    println("Name: $name")
    println("Address: $add")
    println("County: $country")
}

Output

Enter Your Name: 
Shivang
Enter Your Address: 
Indore, sector 73
Enter Your Country Name : 
India
Name: Shivang
Address: Indore, sector 73
County: India

In the above program, we are asking the values for name, address and country. All three values are strings and they are reading by readLine() method.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.