The Scanner class lives in the java.util package, so you import it with a single line at the top of your file
To use Scanner in Java, you need to tell the compiler where to find it. The Scanner class is part of the standard library, bundled with every Java installation. You import it by adding import java.util.Scanner; at the very beginning of your Java file, before your class declaration.
That one line gives your code access to Scanner and all its methods. Without it, the compiler will throw an error when you try to create a Scanner object. The import statement is not optional—it is how Java knows you want to use this particular class instead of writing the full path every time.
Key Takeaways
- Add import java.util.Scanner; at the top of your Java file, before the class declaration.
- The import statement must come before any class or method definitions in the file.
- You can import Scanner by itself or import the entire java.util package using import java.util.*;, though importing the specific class is clearer.
- Once imported, you create a Scanner object with new Scanner(System.in) to read from the keyboard.
- If you forget the import, the compiler will report an error saying the Scanner symbol cannot be found.
Where the import statement goes in your file
The import statement must sit at the very top of your Java file, before anything else except comments. Here is the correct order: package declaration (if you have one), then imports, then your class definition.
Here is what a correct file structure looks like:
import java.util.Scanner; public class MyProgram { public static void main(String[] args) { Scanner input = new Scanner(System.in); } }
The import comes first. The class definition comes after. If you put the import inside the class or inside a method, the compiler will reject it. Java is strict about this order.
Importing Scanner versus importing the entire java.util package
You have two ways to import Scanner. The first is import java.util.Scanner;, which imports only the Scanner class. The second is import java.util.*;, which imports everything in the java.util package at once.
Most programmers use the specific import—import java.util.Scanner;—because it makes your code clearer. Anyone reading your file can see exactly which classes you are using. The wildcard import import java.util.*; works, but it brings in dozens of classes you may never use, and it can make naming conflicts harder to spot if two packages contain a class with the same name.
For learning and small programs, either approach works. For larger projects and professional code, the specific import is the standard choice.
What happens if you forget the import
If you try to use Scanner without importing it, the Java compiler will stop and report an error. The message will say something like "cannot find symbol" or "Scanner is not defined." This error appears at compile time, before your program runs, so you will catch it when ready.
The fix is always the same: add the import line at the top of your file and recompile. Once the import is there, the compiler knows where to find the Scanner class and your code will compile successfully.
Creating a Scanner object after importing
Once you have imported Scanner, you create an object that reads input. The most common way is to read from the keyboard using System.in:
Scanner input = new Scanner(System.in);
This line creates a new Scanner object called input that listens to keyboard input. You can then call methods on it like input.nextLine() to read a full line of text, or input.nextInt() to read an integer. The Scanner object stays open until you close it with input.close().
You can also create a Scanner that reads from a file instead of the keyboard, but you will still need the same import statement at the top of your file.
Common mistakes when importing Scanner
The most frequent mistake is forgetting the import entirely and then wondering why the compiler complains. The second is putting the import in the wrong place—inside a method or inside the class body instead of at the top of the file. Java will reject both.
Another mistake is typing the package name wrong. It is java.util, not java.utils or java.utility. The package name must be exact, or the compiler will not find it.
A less common but real mistake is importing a different Scanner class from a different package by accident. If you have multiple Scanner classes available (which is rare), make sure you are importing java.util.Scanner and not something else. The java.util version is the standard one for reading input.
Frequently Asked Questions
Can I use Scanner without importing it?
No. You must import Scanner before you use it. You could write the full path every time—java.util.Scanner input = new java.util.Scanner(System.in);—but that is awkward and nobody does it. The import statement is the standard way.
What if I import java.util.* instead of just Scanner?
It works fine. The wildcard import brings in all classes from the java.util package, including Scanner. Most programmers avoid it because it clutters your namespace, but for small programs it causes no harm.
Do I need to import Scanner every time I create a new file?
Yes. Each Java file is separate, and each one needs its own import statements. If you create a new class in a new file and want to use Scanner, you must add the import line to that file too.
What does the error "cannot find symbol: class Scanner" mean?
It means you tried to use Scanner without importing it. Add import java.util.Scanner; at the top of your file and recompile. The error will go away.
Can I import Scanner from a different package?
No. Scanner is defined only in java.util. If you try to import it from anywhere else, the compiler will not find it. Always use import java.util.Scanner;.