Hello friends and welcome back. In this lecture we will solve our first exercise about methods. So here's the exercise.
I want to write a method that gets the name of the user and a method that gets the age of the user. So pause the video and try to do this. So let's go to IntelliJ.
So let's start with the getName method. It is going to be a public and a static method. Now let's think about the return type.
This method should get the name of the user. So it is going to return a string, right? Now let's call it getName and it takes no parameters.
And in here, we want to read the name of the user and return it. So we need a scanner object. So let's create a scanner object.
Scanner input is equal to a new scanner and we will pass system.in, alright? After that, we'll get the name of the user. So let's say string name is equal to input.nextLine, alright?
And finally, we will return this name. So we created the scanner object, we read the name from the user, and finally we returned the name. Now let's write this code in another way. I'm going to get this code over here, and then I will remove this statement. And I'm going to return input that next line directly.
So I will read the name of the user and then I will return it. Alright? Also let me show you another way of writing this code. Since we are using the scanner object only one time, we don't need to create a variable that references the scanner object.
So, I'm going to get this code over here and I will remove this statement. And now, over here, I'm going to create a scanner object and immediately I will use the next line method. So, what I need you to know is the following.
If you want to use an object only one time, you can create it like this and immediately use it. You don't need to create a separate variable that references this object. Okay?
Now, let's use this getName method in the main method. I'm going to print enter your name, for example. Alright? And let's use the print method. And after that, I'm going to print the getName method.
So we will tell the user to enter his name. And then we will execute this method. And we are printing the return value of this method. So when we execute this method, we are creating a new scanner object. And we are using the nextLine method to read a string from the user.
And after that, we will return it. So we are returning the string over here. And we are printing it. Alright? Let's run the program.
And as you can see, enter your name. So I'm going to enter my name and press enter and here is my name printed. Now let's write the getAge method.
So this method will also be a public and static method. And if you want the age to be an integer, you can return an integer. And if you want it to be a double, you can return a double. In this case, I will return a double. Let's call it getAge.
And in here, we will do the same thing. We will return a new scanner and we will pass system.end. And after that, we will call the next double method.
All right. So we are creating a new scanner object and reading the age of the user and returning it Now let's use this method in the main method So over here let's say enter your name and age And over here let's print the name of the user concatenated with the age of the user So let's concatenate a space and then call the getAge method So we will get the name of the user concatenate a space and then we will concatenate the age of the user Let's run the program So first of all, I will enter my name, press enter, and now this is stored inside the string, because we are using the next line method. And now we are reading the age. So I'm going to enter my age, and press enter, and this is the output, alright? So this is it, thanks for watching, and I'll see you in the next video.