Kotlin Input Output

Kotlin input output operations performed to flow sequence of bytes or byte streams from input device like keyboard to main memory  and from the main memory to output device like Monitor.

Kotlin Output – You can use the following function to display output of any datatype  on the screen. 

print() function it is used to print values inside the method “()”.
println() function it is used to print values inside the method “()” moves the cursor to the beginning of the next line.

fun main()
{
    val x = 23 
    val name = "Justin"
    println("Hi")
    print("Hola")
    println(" x = $x")
    print("Hi ${name}")
}

output
Hi
Hola x = 23
Hi Justin

Kotlin Input – Input can be taken by following function

  • readline() method
  • Scanner class

Taking input using readLine() method –

readLine()- It is used for reads line of string input from standard input stream

fun main(args : Array<String>) {
    print("Enter your name: ")
    var name  = readLine()
    print("You're name is $name")
}

output
Enter your name: Coding Friction 
You're name is Coding Friction

Taking input using Scanner class – To take input other data types other than String, we need to use the Scanner object of java.util.Scanner. to use you need to import first.

import java.util.Scanner  
fun main(args: Array<String>) {  
    val readMarks = Scanner(System.`in`)  
    println("Enter your marks")  
    var marks = readMarks.nextInt()  
    println("Your marks is "+marks )  
}  

output 
Enter your marks
23
Your marks is 23