Console Input in Android App Development

Start course

This course takes a deep dive into Kotlin operators, giving you a practical understanding of how to use operators in your code.

Intended Audience

This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.


This content will take you from a beginner to a proficient user of Kotlin and so no prior experience with the programming language is required. It would, however, be beneficial to have some development experience in general.


Well, hello, my friends. So, in this video, we're going to learn how to enter data into the console and get the data entered by the user. Alright, so far we've learned fundamentally different information about Kotlin and we've only seen the output from the console. However, we can have the user enter some data into the console and assign that data entered into a variable. You follow?

So, it's really simple to do this in Kotlin. So, we use the readLine() method for the user to enter data into the console. By using this method, we can get the data that the user enters into the console. Okay, so of course, we can do the action that we want to by transferring this data to a variable. Now, I would also like to partially touch on, if you will, the nullable issue here. So, normally you would ask the user to enter some data, but let's assume that the user presses the 'Enter' key without entering any data. In that case, you wouldn't have any data and the program would throw up an error. But in Kotlin, in order to avoid this problem, all we need to do is use the readLine() method in two different ways.

So, first when we transfer the data we received from the user to the variable, we need to put a ? next to the data type of the variable. So, this means that the variable we defined may or may not contain data. Because we ask the user to enter data and the user may not actually enter any data. We're not actually going to know, but we don't want to throw up an error. So, in this case, even if the user doesn't enter any data, we can prevent the program from failing. Now, a second spelling is to put two ! at the end of the readLine() method. So, this means that we're completely sure that the user is entering data. That is even if the user does not enter any data, the program will assume that the user has entered some data and an error can be avoided that way too. We're going to learn about that situation more in detail in our coming lessons. But I just wanted to, like I said, briefly touch on the use of the readLine() method, so that you can start to let it sink in a little. Now I do want to mention one more thing. The data that we receive from the user is always of the type string by default.

And you know what, actually I'll just explain this with a short example. So, let's say that you will develop a calculator application. So, in this case, the user must enter values of the type integer or double, right? However, even if the user enters data of the type integer or double, we cannot operate using this data directly. Why? Because this data is taken from the user in a string type. So, in that case, we'll need type conversions. As a result, if the user enters data other than string data type, well, first of all, we must convert this data to integer or double according to our application. Then we can use this data as we want to.

So why don't we do this? Let's go to Android Studio now and do some practice. So first, I will create a new Kotlin file. I'll set the name of this file to be ReadLine. Now let's create our main method. Now, one of these things is, why don't we ask the user to enter their name and their age? And then, after the user enters this information, let's print the information entered by the user to the console. So, first let's ask the user for their name using the print method. Now, write, what is your name in paranthesis. You can also use the printIn() method here. If you use the print method, the cursor will not move to the next line when the user writes their name on the console. They will have typed it against the expression here. You follow? So, if you use the printIn() method, the cursor will move to the next line and we'll have written the username below the expression here. I mean that choice is really up to you but you've got to know the difference. Anyway, now let's transfer the user's name to a variable named name and the type string using the readLine() method. All right, var name:String? = readLine(). So, we've now assigned the name of the user to the variable named name. Now, let's ask the user's age, so I can use the print() method again. All right, "How old are you" in parenthesis. Now, let's transfer the user's name to a variable named age and type integer. And, we can use the readLine() method once again. Okay, so I write var age:Int? = readLine().

Now, you know what? Let's create it with a second method too. So, let me just delete the question mark here. I'll put two ! at the end of the readLine() method. Now pay attention to this. See how the code editor warns us? It's because the variable named age is of the type integer. But the data type that we get from the user is of the string type. So, in another words we are trying to pass a string data type to an integer variable. And of course, our trusty code editor warns us. So, let's convert the string data type that we received from the user to energy type. Of course, you remember, we used the toInt method for this. So here I'll write .toInt, and sure enough, as you see, the warning's vanished. So we took the user's name and age, passed it to variables. Now, let's print the user's name and age to the console. So, I write sout, press 'Enter' and in paranthesis, I'll write, "Your name is" and put the $ sign, write the name variable. Remember, this way of writing allows us to write code inside a string expression. Good, so let's print the age of the user to the console, all right? And your age is, and here too, let's combine the two expressions using the + sign instead of the $ sign. So, I'll write + age. See, so I've shown you both in my clever way, both spelling styles. You can either print something to the console using a $ sign or you can print using a + sign. The choice really is entirely yours, my friends. But I just want to show you how clever I am. So there we are. You know what?

What occurs to me is some people use the expression, "Killing two birds with one stone." Well, I don't know, that just strikes me as not being very appropriate. So, my friends, let's now run our code. As you can see, there is an expression in the console asking for a name. So, let's write in our name here. So, I'll write David, and after typing the name, press the 'Enter' key on the keyboard. Next action should take place. So, this time there is an expression on the console asking for our age. So, that's in our age here, write 25, I press 'Enter', and once again, as you can see on the console, name and age are printed. Well, there you have it, my friends, getting data from the user using the readLine() method. We are definitely going to be using this method very often in our coming lessons. So, why don't we take a short break here? And in our next lesson, we're going to learn control flow expressions. See, we're building, and building, and building. All right, see you in the next video.


About the Author
Learning Paths

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics