The Scanner class in Java can help us receive data input from the keyboard. The following is an example program that Bing brought me.
Operation method
First, we create a ScannerDemo class with a class name under a java project.
Then we create a Scanner class object to receive the data input from the keyboard.
Here we use the nextLine method to receive strings. The next method can also obtain strings. However, the next method cannot obtain blank spaces. For example, if you enter Hello World, you can only get Hello using the next method while you can get the complete Hello World using the nextLine method. Because the nextLine method ends with the Enter key.
Right click in the blank area of the program window and select Run As -- Java Application
At this time, in the console, enter a string and press Enter. You can see that the string is successfully received using the nextLine of the Scanner class.
Of course, in addition to receiving strings, Scanner can also receive integer and floating point numbers, which have corresponding methods. Let's take receiving integers as an example. Other types are also similar. Let's try it yourself.
Run to view the results. You can see that nextInt() can only receive integers. If you want to receive other types of data, you have to use other methods. If you don't understand, you can go to the document.