This class is found in the java.util package. There are various ways to read input from the keyboard, the java.util.Scanner class is one of them. This works by breaking the input into tokens using a delimiter which is whitespace by default. Many methods are provided to read and parse various primitive values. It is widely used to parse text for strings and primitive types using a regular expression. It is the easiest way to get input in Java.

Syntax for declaring java.util.Scanner class:

public final class Scanner extends Object implements Iterator


Com.knowledge2life import java.util.Scanner; class Main { public static void main(String[] args) { Scanner myObj = new Scanner(; String userName; System.out.println("Enter username"); userName = myObj.nextLine(); System.out.println("Username is: " + userName); } }


Enter username (Knowledge2life)
Name is: Knowledge2life