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(...)').
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.
So far, we have used variables to store values. But the variables were loaded with values.
fun main() { var x = 9 println("The value of x is : ",x) }
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.
fun main() { print("Enter any text : ") var x = readLine() println("The text you have entered is : "+x) }
Now, if you see the Output. The first line says,
Now, let us go to the above code and see what happened?
In the above code, we have used the print statement,
Then we have declared a variable 'x', which is initially empty.
So, what happened is, the below String is Printed,
Then we have the statement,
'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.
In the above case, I have typed 'Hello'.
And 'Hello' goes and sits inside 'x'.
And we got the output,
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.
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) }
Now, if you see the Output. The first line says,
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,
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'.
Then we have used the print statement,
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.
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.
The statement,
'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.
In the above case, I have typed '21'.
And '21' goes and sits inside 'x'.
And we got the output,
And 'nextInt()' makes sure that the user enters nothing other tha a number.
Next, let us look at Kotlin Output.
A simple example of 'println(...)' statement would be,
fun main() { var x = 9 println("The value of x is : "+x) }
With the 'println(...)' statement, Kotlin supports something called as 'String Templates'.
Let us dive into an example to understand 'String Templates'.
fun main() { var x = 5 var y = 8 var z = x + y println("The added result of ${x} and ${y} is ${z}") }
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 '${}'.
And what happens is, the variables 'x', 'y' and 'z' gets replaced with actual values i.e. '5', '8' and '13'.