The Scanner class lives in the java.util package
To use Scanner in your Java program, you need to import it from the java.util package at the top of your file. The import statement tells Java where to find the Scanner class so you can create Scanner objects and read input from the keyboard, files, or other sources.
The simplest way is to write this single line at the very beginning of your Java file, before your class declaration:
import java.util.Scanner;
That one line is all you need. After that, you can create a Scanner object anywhere in your code and start reading user input. Without this import statement, Java will not know where to find the Scanner class and your code will fail to compile.
Key Takeaways
- Place import java.util.Scanner; at the top of your Java file, before the class declaration.
- The import statement must come before you try to create a Scanner object or the code will not compile.
- You can import just Scanner, or use a wildcard import to bring in all classes from java.util at once.
- Once imported, create a Scanner object with Scanner input = new Scanner(System.in); to read from the keyboard.
- Calling input.close() when you finish using Scanner frees up system resources and is considered good practice.
Where to place the import statement in your file
The import statement must go at the very top of your Java file, before anything else except comments or the package declaration (if your file belongs to a package). Java reads the file from top to bottom and needs to know where Scanner lives before it encounters the word "Scanner" in your code.
Here is the correct order for a typical file:
- Package declaration (if you have one)
- Import statements
- Class declaration
- Methods and code
If your file does not belong to a package, the import statement is straightforward the first line. Any comments you write above the import are fine — Java ignores those — but no executable code should come before the import.
Single import versus wildcard import
You have two ways to import from java.util. The first, import java.util.Scanner;, brings in only the Scanner class. This is the most common choice because it is clear and specific about what you are using.
The second option is a wildcard import: import java.util.*; This brings in every class in the java.util package at once. It saves typing if you need multiple classes from that package, but it can make your code harder to read because someone looking at the file cannot when ready see which classes you actually use.
Most Java programmers prefer the single import for Scanner unless they are already importing other classes from java.util. The single import also makes it easier to track dependencies and understand what your code relies on.
Creating a Scanner object after importing
Once you have imported Scanner, you can create a Scanner object. The most common way is to read from the keyboard (called standard input):
Scanner input = new Scanner(System.in);
This creates a Scanner object named input that listens to what the user types. You can then use methods like nextLine() to read a full line of text, nextInt() to read a whole number, or next() to read a single word.
You can also create a Scanner that reads from a file instead of the keyboard, but that requires importing the File class as well and handling exceptions — a more advanced topic that builds on the basics of Scanner import.
A complete working example
Here is a short program that shows the import statement in context:
import java.util.Scanner;
public class ReadUserInput { 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. After you finish using the Scanner, it is good practice to call input.close() to free up system resources, as shown on line 8. This tells Java that you are done with the Scanner and it can release the memory and file handles it was holding.
What happens if you forget the import
If you try to use Scanner without importing it, your code will not compile. The Java compiler will show an error message saying something like "cannot find symbol: class Scanner" or "Scanner is not defined." This error means Java looked for Scanner in the current file and in the default packages, but did not find it because you did not tell Java where to look.
The fix is always the same: add the import statement at the top of your file. Once you do, the error disappears and the code compiles. If you see this error, check that the import line is actually at the top of the file and that you spelled "Scanner" correctly in both the import and in your code.
Frequently Asked Questions
Can I use Scanner without importing it?
No. You must import Scanner from java.util before you can use it. The only exception is if you use the full name every time: java.util.Scanner input = new java.util.Scanner(System.in); This is awkward and not recommended, but it works if you forget the import.
Do I need to import System.in as well?
No. System and System.in are part of the java.lang package, which Java imports automatically for every program. You only need to import Scanner.
What if I want to read from a file instead of the keyboard?
You still import Scanner the same way. To read from a file, you also need to import java.io.File and handle exceptions, which is more complex. Start with keyboard input using System.in first, then move to file reading once you are comfortable with Scanner.
Is import java.util.* a bad practice?
It is not forbidden, but most professional Java code uses single imports like import java.util.Scanner; because it makes the code clearer. Wildcard imports can hide which classes you actually depend on, making the code harder to understand and maintain.
Do I have to call input.close() when I am done?
Technically, your program will work without it, but calling input.close() is considered good practice. It tells Java to release the resources the Scanner is using. In small programs it does not matter much, but in larger applications or when reading from files, closing resources prevents memory leaks and other problems.