Skip Navigation

Creating an instance of the Scanner class

Your textbook may say to use the Scanner.create() method to create an instance of the Scanner class. This is no longer the way to do it. You should create an instance of the Scanner class in the same way you would create an instance of any other class, by using a constructor.

Incorrect:

Scanner myScanner = Scanner.create(System.in);

Correct:

Scanner myScanner = new Scanner(System.in);

Example:

import java.util.*;

/**
* A simple example of using the Scanner class.
*/
class EchoYourInfo
{
/**
* Echos the information you enter.
*/
public static void main(String[] args)
{
String firstName;
Scanner myScanner = new Scanner(System.in);

System.out.println("Enter your first name:");
firstName = myScanner.next();
System.out.println("Hi " + firstName);
}
}