Skip to main content

Kotlin Input/Output

Output mechanism are used to display information of results and input mechanism are used to input the value by keyboard when running of kotlin program. there is fews standard method are available in kotlin programming to do this task.

Display Outputs

In kotlin programing there us two functions are available to display information of output console. that is print() and println() function. We can display similar information in multiple way in kotlin programming. for example.

fun main(args: Array<String>){
   
   val dataInfo:Int=10
   println(dataInfo) //simple way
   println("$dataInfo") //using template
   println("${dataInfo*2}")//multiply value
   //add value by plus() kotlin function
   println("${dataInfo.plus(12)}") 
}

Output

10
10
20
22

Get Inputs

kotlin is providing standard library readLine() to deal of input mechanism. that is useful to get the value of keyboard.

fun main(args: Array<String>) {  
    println("Enter your favorite programming language")  
    val language = readLine()  
    println("your favorite programming language is $language")  
}  

Output

Enter your favorite programming language
Kotlin
your favorite programming language is Kotlin

Convert Input Data to Other Data type

When user is input value, readLine() is always return a string. We can convert this string to integer and float value in following way.

fun main(args: Array<String>) {

  print("Input Integer Value :")
  //Input string value and convert to Integer
  val intValue=Integer.valueOf(readLine())
  
  print("Input Float Value :")
  //Input string value and convert to Float
  val floatData=readLine()!!.toFloat()

  //display input values
  println("intValue:$intValue \nfloatData:$floatData")
  
  //display class
  println(intValue.javaClass.name)
  println(floatData.javaClass.name)
}

Output

Input Integer Value :10
Input Float Value :45.89
intValue:10 
floatData:45.89
java.lang.Integer
float

In this both method there are need to input valid string integer and float value. see one more example to convert all other data type.

fun main(args: Array<String>) {

  print("Input Byte Value :")
  //Input string value and convert to Byte
  val byteData=readLine()!!.toByte()


  print("Input Short Value :")
  //Input string value and convert to Short
  val shortData=readLine()!!.toShort()

  
  print("Input Int Value :")
  //Input string value and convert to Int
  val intData=readLine()!!.toInt()


  print("Input Long Int Value :")
  //Input string value and convert to Long int
  val longData=readLine()!!.toLong()

  
  print("Input Float Value :")
  //Input string value and convert to Float
  val floatData=readLine()!!.toFloat()

  print("Input Double Value :")
  //Input string value and convert to Double
  val doubleData=readLine()!!.toDouble()

  //display input values
  println("byteData   : $byteData")
  println("shortData  : $shortData")
  println("intData    : $intData")
  println("longData   : $longData")
  println("floatData  : $floatData")
  println("doubleData : $doubleData")
  

  //display class
  println("Class Info")
  println("byteData   : ${byteData.javaClass.name}")
  println("shortData  : ${shortData.javaClass.name}")
  println("intData    : ${intData.javaClass.name}")
  println("longData   : ${longData.javaClass.name}")
  println("floatData  : ${floatData.javaClass.name}")
  println("doubleData : ${doubleData.javaClass.name}")
 
}

Output

Input Byte Value :1
Input Short Value :1000
Input Int Value :5676
Input Long Int Value :88888888888
Input Float Value :67.34
Input Double Value :134.23
byteData   : 1
shortData  : 1000
intData    : 5676
longData   : 88888888888
floatData  : 67.34
doubleData : 134.23
Class Info
byteData   : byte
shortData  : short
intData    : int
longData   : long
floatData  : float
doubleData : double

This is kotlin type conversion to one type to another. we are also use java Scanner to input in kotlin. see this example.

import java.util.Scanner
 
fun main(args: Array<String>) {
 
   // Create object of scanner class
   val reader = Scanner(System.`in`)
   
   print("Input a Byte value: ")
   var byteValue: Byte = reader.nextByte()

   print("Input a Short value: ")
   var shortValue: Short = reader.nextShort()

   print("Input a Integer value: ")
   var intValue: Int = reader.nextInt()

   print("Input a Boolean value: ")
   var booleanValue: Boolean = reader.nextBoolean()

   print("Input a Long value: ")
   //accept Long value in input
   var longValue: Long = reader.nextLong()

   print("Input a Float value: ")
   var floatValue: Float = reader.nextFloat()


   print("Input a Double value: ")
   //accept double value in input
   var doubleValue: Double = reader.nextDouble()
 
   //display input values
   println("byteValue   : $byteValue")
   println("shortValue  : $shortValue")
   println("intValue    : $intValue")
   println("booleanValue: $booleanValue")
   println("longValue   : $longValue")
   println("floatValue  : $floatValue")
   println("doubleValue : $doubleValue")
}

Output

Input a Byte value: 10
Input a Short value: 100
Input a Integer value: 1284
Input a Boolean value: True
Input a Long value: 10000000
Input a Float value: 56.22
Input a Double value: 1234.222
byteValue   : 10
shortValue  : 100
intValue    : 1284
booleanValue: true
longValue   : 10000000
floatValue  : 56.22
doubleValue : 1234.222




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment