The Scanner class is a class in java.util, which allows the user to give an input of various types. Which Scanner options do you have in Java?

There are two constructors:

  1. Scanner in = new Scanner(System.in); This is used when the program wants an input from the user.
  2. Scanner inFile = new Scanner(new FileReader(“myFile”)); This is used when the input could found in a file.

The following methods exist in the Scanner class:

Method

Returns

.nextInt()

Returns the next token as an int.

.nextLong()

Returns the next token as a double.

.nextFloat()

Returns the next token as a float.

.nextDouble()

Returns the next token as a double.

.next()

Returns the next token as a String.

.nextLine()

Returns a whole line as String.

.close()

Closes the scanner.

Click here to find out when do you have to use the scanner.
Click here for different methods in the scanner class.