Import the Scanner class with a single line at the top of your file

To use Scanner in Java, write import java.util.Scanner; at the very beginning of your code file, before any class declaration. This one line gives your program access to all Scanner methods — nextLine(), nextInt(), next(), and the rest. Without this import statement, Java will not recognize the word "Scanner" and your code will not compile.

The import statement must come before your class definition. If you have a package declaration (like package myapp;), the import goes after that but before the class starts. The order matters: package first, then imports, then your class.

Key Takeaways

  • Place import java.util.Scanner; at the top of your file, after any package statement but before your class declaration.
  • Scanner lives in the java.util package, which is why the import path includes that full location.
  • Once imported, you create a Scanner object with new Scanner(System.in) to read from the keyboard.
  • You can import Scanner individually or import the entire java.util package with import java.util.*;, though naming the specific class is clearer.

Where Scanner lives and why you need to import it

Java organizes classes into packages — folders of related code. Scanner is not in the default package that Java loads automatically. It lives in java.util, a package that holds utility classes like ArrayList, HashMap, and Random. To use any class from a non-default package, you must tell Java where to find it with an import statement.

If you try to use Scanner without importing it, the compiler will throw an error: "cannot find symbol — class Scanner". The error means Java looked for Scanner in the default package and did not find it. The import statement solves this by pointing Java to java.util.Scanner.

The correct placement of your import statement

Your import must sit at the top of the file, in this order: package declaration (if you have one), then imports, then your class definition. Here is the structure:

package myapp; (optional — only if your class belongs to a package) import java.util.Scanner; (required to use Scanner) public class MyProgram { ... } (your class definition starts here)

If your file has no package declaration, the import is the first line. If you have multiple imports, list them one per line. Java does not care about the order of imports, but putting them together at the top makes the file easier to read.

Using Scanner after you import it

Once you have imported Scanner, create an instance of it to read input. The most common way is to read from the keyboard using System.in:

Scanner input = new Scanner(System.in);

This line creates a Scanner object called input that listens to keyboard input. You can then call methods on it: input.nextLine() reads an entire line of text, input.nextInt() reads the next integer, and input.next() reads the next word. Always close the Scanner when you are done with input.close(); to free up system resources.

Importing the whole package versus importing one class

You have two ways to import from java.util. The first is to name Scanner specifically: import java.util.Scanner; This imports only Scanner and makes your imports list clear about what you are using.

The second is to import everything in the package: import java.util.*; The asterisk is a wildcard that brings in Scanner, ArrayList, HashMap, and every other class in java.util. This saves typing if you use many classes from the same package, but it makes your code less transparent — a reader cannot tell at a glance which classes you actually use. Most Java programmers prefer naming the specific class.

Common mistakes when importing Scanner

The most frequent error is forgetting the import entirely and trying to use Scanner without it. The compiler will reject your code with "cannot find symbol". The fix is always to add the import line at the top.

Another mistake is putting the import in the wrong place — for example, inside your class or after your class declaration. Java expects imports to come before any class definition. If you put an import inside a method, Java will treat it as a syntax error.

A third mistake is misspelling the package name. import java.util.Scanner; is correct. import java.utils.Scanner; (with an 's' on util) will fail because that package does not exist. Double-check the spelling: it is java.util, not java.utils.

A complete working example

Here is a short program that imports Scanner and uses it to read a name from the keyboard:

import java.util.Scanner; public class Greeting {   public static void main(String[] args) {     Scanner input = new Scanner(System.in);     System.out.println("What is your name?");     String name = input.nextLine();     System.out.println("Hello, " + name);     input.close();   } }

The import statement is on line 1. The Scanner object is created on line 4. The program reads a line of text with nextLine() and prints it back. The close() method at the end releases the Scanner's resources.

Frequently Asked Questions

What happens if I use Scanner without importing it?

Java will not compile your code. The compiler will print an error message: "cannot find symbol — class Scanner". You must add import java.util.Scanner; at the top of your file before the code will run.

Can I import Scanner inside a method?

No. Import statements must appear at the top of the file, before your class definition. If you put an import inside a method or class body, Java will treat it as a syntax error and refuse to compile.

Is it better to import Scanner specifically or use import java.util.*?

Importing Scanner specifically is clearer and more common. import java.util.Scanner; tells anyone reading your code exactly which class you are using. The wildcard import java.util.*; works but hides which classes you actually need.

Do I need to import Scanner if I use it in multiple classes?

Yes. Each Java file that uses Scanner must have its own import statement at the top. If you have two classes in two separate files, both files need import java.util.Scanner;

What is System.in and why do I use it with Scanner?

System.in is Java's standard input stream — the keyboard. When you write new Scanner(System.in), you are telling Scanner to read from the keyboard. You can also create a Scanner that reads from a file or a string, but System.in is the most common choice for programs that interact with users.