The Scanner class is imported with a single line at the top of your Java file

To use the Scanner class in Java, add this line before your class definition:

import java.util.Scanner;

The Scanner class lives in the java.util package, which is why you need to import it. Once imported, you can create a Scanner object to read input from the keyboard, files, or other sources. Without this import statement, Java will not recognize the Scanner class name and your code will not compile.

The import statement must appear at the very top of your file, before any class declarations. You can have multiple import statements, and they all go in the same section.

Key Takeaways

  • Add import java.util.Scanner; at the top of your Java file, before the class declaration.
  • Scanner reads input from the keyboard using System.in, or from files using a File object.
  • Common Scanner methods include nextLine() for text, nextInt() for whole numbers, and nextDouble() for decimals.
  • Always close the Scanner when you are done reading input by calling scanner.close() to free system resources.

Creating a Scanner object after importing

Once you have imported Scanner, create an instance of it inside your method. The most common way is to read from keyboard input:

Scanner scanner = new Scanner(System.in);

This creates a Scanner object named scanner that listens to System.in (the keyboard). You can name the object anything you want — input, reader, or sc are all common choices. The name does not matter; what matters is that you use that same name when you call Scanner methods.

If you want to read from a file instead of the keyboard, you would write:

Scanner scanner = new Scanner(new File("filename.txt"));

This requires importing the File class as well: import java.io.File;

Reading different types of input with Scanner methods

Scanner provides different methods depending on what type of data you want to read. The method you choose determines what the Scanner expects to find and what type of value it returns to you.

nextLine() reads an entire line of text, including spaces, and stops when the user presses Enter. Use this when you want to read a full sentence or any text that might contain spaces.

nextInt() reads a whole number and skips any whitespace before it. If the user types text instead of a number, the program will crash with an InputMismatchException.

nextDouble() reads a decimal number. Like nextInt(), it will crash if the user enters non-numeric input.

next() reads a single word (stops at the first space) without the newline character.

A complete example with Scanner

Here is a working program that imports and uses Scanner:

import java.util.Scanner;

public class UserInput {   public static void main(String[] args) {     Scanner scanner = new Scanner(System.in);     System.out.println("Enter your name:");     String name = scanner.nextLine();     System.out.println("Enter your age:");     int age = scanner.nextInt();     System.out.println("Hello " + name + ", you are " + age + " years old.");     scanner.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 stores it in the variable name. Then it reads an integer with nextInt() and stores it in age. Finally, it closes the Scanner on line 10 to release the resource.

Why you should close the Scanner when you are done

Calling scanner.close() tells Java that you are finished reading input and frees up the system resource that Scanner was using. If you create many Scanner objects without closing them, your program can run out of resources and slow down or crash.

In small practice programs, forgetting to close Scanner usually does not cause a visible problem. In larger applications or programs that run for a long time, it matters. The habit of closing resources is important for writing reliable code.

If you are reading from a file, closing the Scanner is even more important because it ensures the file is properly closed and other programs can access it if needed.

Common mistakes when importing Scanner

The most common mistake is forgetting the import statement entirely. If you write Scanner scanner = new Scanner(System.in); without the import, you will see an error like "cannot find symbol: class Scanner" when you try to compile.

Another mistake is importing the wrong package. Java has multiple Scanner-like classes, but the one you want is always java.util.Scanner. If you accidentally import from a different package, the class will not work the way you expect.

A third mistake is trying to read the wrong type of data. If you call nextInt() but the user types "hello", the program crashes. You can prevent this by checking what the user typed first using methods like hasNextInt(), but that is a more advanced topic.

Frequently Asked Questions

Do I need to import Scanner every time I use it?

No. You import it once at the top of each Java file where you want to use it. That single import statement makes Scanner available to all classes and methods in that file.

Can I use Scanner to read from a file?

Yes. Instead of new Scanner(System.in), write new Scanner(new File("filename.txt")). You will also need to import the File class from java.io. If the file does not exist, Java will throw a FileNotFoundException.

What happens if the user enters the wrong type of data?

If you call nextInt() but the user types text, the program throws an InputMismatchException and stops. To handle this safely, use hasNextInt() to check before reading, or use a try-catch block to catch the exception.

Is Scanner the only way to read input in Java?

No. You can also use BufferedReader, which is faster for large amounts of input, or System.console() for console-based programs. Scanner is the most beginner-friendly option and works well for most situations.