Let’s now move to the next video where you will learn how your code can ask a user to input values of his/her choice.
You can download the following java file which will be very useful to you while watching the video. Right click on the java file and open it with Intellij. Please be careful with the name of the java file. The name of the java file should exactly be same as the class name inside it for the code to run. So make changes accordingly.
Let us now see how we can take a user input. So basically for taking a simple user input, first you have to write a statement import Java util scanner. For now, you don't need to worry about what this statement is doing, but rather just accept that you have to write this statement in order to be able to take a user input. So now we say we declare a public class user input and inside this we write the public static void main statement. Then I declare two simple variables int number A and int number B. Then I say scanner input is equal to new scanner system in. So basically what this statement does is that it creates a new variable object called input which belongs to the scanner class. You don't need to worry about what the scanner class is, but just assume that creating this object is necessary to be able to take the user input. And it is only this object which will facilitate us taking the input from the user. So I say scanner input is equal to new scanner system in. This is a standard way of declaring a new object through which we can take the user input. Then we print on the console, enter the value for A. Then I say number A is equal to input next int. So basically next int is an inbuilt function of this particular scanner object. You don't need to worry about it, but just assume that this function helps to take in the user input. So this function takes in the user input and puts it inside the variable number A. Similarly, I then print on the console, enter the value for B and I do a similar operation. I call the next int function on my input object and whatever the user inputs I store it inside number B. And then finally, after taking input number A and number B from the user, I print their addition here. So let us try to run this file and see how it works. So when I run, as you can see, first enter the value for a statement is printed. So now the user is supposed to input a value for A. Suppose I am inputting four, then I press enter. Then I get the second print statement which says enter the value for B. Now you must enter the value for b. Suppose I enter five here and press the enter key. So now you can see that nine is getting printed on the console. So this nine here actually comes from the addition of number A and number B. So basically what happens is that once you enter four here on the console, the next int function is getting called and this four is getting stored inside number A. Similarly, when you enter five here on the console, again this five is getting stored inside the number B variable by calling the next int function on input. Finally we print the addition of number A and number B. So in this way, we have successfully been able to take input from the user and print the addition of any two numbers which are input by the user.
To take a user input in Java, you need to write the statement "import Java util scanner".
Declare a public class "user input" and write the public static void main statement.
Declare two variables: int number A and int number B.
Create a new variable object called "input" belonging to the scanner class by writing "scanner input is equal to new scanner system in".
Print on the console "enter the value for A" and use the "next int" function to take the user input and store it inside the variable number A.
Similarly, print on the console "enter the value for B" and use the "next int" function to take the user input and store it inside the variable number B.
Finally, print the addition of number A and number B.
To run the file, input the value for A, press enter, input the value for B, press enter, and the sum of A and B will be printed on the console.
This is a simple way to take user input and perform an operation in Java.
That was interesting! You used the Scanner class to prompt the user to input the values.
Explanation of the Scanner Code
First you wrote the following statement to be able to use the Scanner class:
Scanner input= new Scanner(System.in);
Here, “input” is the name of your Scanner. You can name the Scanner absolutely anything(like choosing variable names).
This statement “new Scanner(System.in)” prompts Java to provide you with a new Scanner to “scan” the input given by users.
You can then use the following statement to read some integer data given as input from the user:
a=input.nextInt();
Here, the “.nextInt()” keywords tell you scanner(named “input”) to look for the next data entered by the user and store it in the variable “a”.
Remember, for Java to know where should it get the code for the Scanner from, you have to add the following statement at the top of your code file:
import java.util.Scanner;
This statement merely tells Java to “import” all the code required to use the Scanner to take user input.
Note: Often, your IDE will automatically add this import statement when you try to use the Scanner in your code.
Being able to take user input will be of great use in the future too, so try to remember it well.