In order to make the user interact with his keyboard, java has a special class called Scanner. To use this class you must:

  • Import the java.util.Scanner package
  • Make an instantiation  :
Tell Java where the Scanner class is located. To do this, we need to import the Scanner class through the import statement. The class we want is in the java.util package.
  •  Guiding the user with a message that appears on the screen:
The Scanner class that is in the java.util package. Replace them. By /, the directory tree is java / util / and in this folder is the file Scanner.class! You will see this in more detail when we make our own packages.

  • Retrieve keyboard input in a Java variable:

  • Final code :

Note

We do not always use nextLine () to retrieve a keyboard entry, the trick we use will be according to the nature of the input:

  • nextInt ()          :   if the input is an integer
  • nextDouble ()   :    if the input is a double variable
  • nextLong ()      :    if the input is a variable of type long ...

Leave a Reply