Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   




KOTLIN - INPUT & OUTPUT


Input and Output are preferably the most important statements in a programming language.


So far, we have seen the Output command of Kotlin(i.e. 'println(...)').


Example :



fun main() {

    println("Hello World") 
}	 

And we are yet to look at the Input statement.


So, let us look at the Input statement. Then we will be exploring a few more ways to use the Output statement.


Kotlin Input


So far, we have used variables to store values. But the variables were loaded with values.


Example :



fun main() {

    var x = 9
    println("The value of x is : ",x)
}


Output :



 The value of x is : 9

But, what if you don't want the variable 'x' to contain a fixed value like '9'. Rather you want the user to enter any value of their choice.


And this is where the Input statements comes to picture.


Just like the Output statement is called 'println()'. Same way the Input statement is called as 'readLine()'.


Let us understand 'readLine()' statement with the below example.


Example :



fun main() {

    print("Enter any text : ")
    var x = readLine()
    println("The text you have entered is : "+x)
}


Output :



 Enter any text : Hello
 The text you have entered is : Hello

Now, if you see the Output. The first line says,


Enter any text : Hello

Now, let us go to the above code and see what happened?


In the above code, we have used the print statement,


print("Enter any text : ")

Then we have declared a variable 'x', which is initially empty.


var x = readLine()


java_Collections

So, what happened is, the below String is Printed,


Enter any text :

Then we have the statement,


var x = readLine()

'readLine()' is used to get an input from the user, and if the user enters a value, it goes and sits inside variable 'x'.


So, the screen is stuck, until user types any number.



java_Collections

In the above case, I have typed 'Hello'.


Enter any text : Hello

And 'Hello' goes and sits inside 'x'.



java_Collections

And we got the output,


The text you have entered is : Hello

So, we have seen how can we get a String/Text as an input.


Now, let us say, you want to get a number as an input.


In that case, we need to use the 'Scanner' class. Let us look at it with the below example.


Example :



import java.util.Scanner

fun main() {
        
    var temp = Scanner(System.`in`)
        
    print("Enter a number of your choice : ")
    var x = temp.nextInt()
    println("The number is : "+x)
}


Output :



 Enter a number of your choice : 21
 The number is : 21

Now, if you see the Output. The first line says,


Enter any number of your choice : 21

Now, let us go to the above code and see what happened?


So, if we take a look a the very first line, it is,


import java.util.Scanner

And this is the Line you must use if you are going to use the Scanner class.


It simply says, in order to use the 'Scanner' class, you must import it first.


In the next line, we have created a variable of type 'Scanner' class and initialised to a variable named 'temp'.


var temp = Scanner(System.`in`)

Then we have used the print statement,


print("Enter a number of your choice : ")

Now comes the fun part in the next line, where we have declared a variable 'x', and used the 'temp' variable of Scanner class (That we have created above) to get a number as an input.


var x = temp.nextInt()

Also we have used 'nextInt()' with 'temp', so that if we enter something other than a number (i.e. A name or a decimal number), it would end up with an error.


Sounds tough?


Let's dissect it.


var x = temp.nextInt()

The statement,


temp.nextInt()

'temp.nextInt()' is used to get an input from the user, and if the user enters a value, it goes and sits inside variable 'x'.


So, the screen is stuck, until user types any number.



java_Collections

In the above case, I have typed '21'.


Enter a number of your choice : 21

And '21' goes and sits inside 'x'.



java_Collections

And we got the output,


The number is : 21

And 'nextInt()' makes sure that the user enters nothing other tha a number.


Next, let us look at Kotlin Output.


Kotlin Output


A simple example of 'println(...)' statement would be,


Example :



fun main() {

    var x = 9
    println("The value of x is : "+x)
}


Output :



 The value of x is : 9

String Templates using '$'


With the 'println(...)' statement, Kotlin supports something called as 'String Templates'.


Let us dive into an example to understand 'String Templates'.


Example :



fun main() {

    var x = 5
    var y = 8
    var z = x + y
    println("The added result of ${x} and ${y} is ${z}")
}	


Output :



 The added result of 5 and 8 is 13

So, in the above example, we have three variables, 'x', 'y' and 'z'.


Now, inside the 'println(...)' statement, we have used 'x', 'y' and 'z' inside '${}'.


println("The added result of ${x} and ${y} is ${z}")

And what happens is, the variables 'x', 'y' and 'z' gets replaced with actual values i.e. '5', '8' and '13'.



java_Collections