Import the Scanner class with a single line at the top of your file
To use the Scanner class in Java, you must import it before you write any code that creates a Scanner object. Add this line at the very beginning of your Java file, before your class declaration:
import java.util.Scanner;
That single line tells Java where to find the Scanner class. Without it, your program will not compile — the compiler will throw an error saying it cannot find the symbol "Scanner". The import statement must appear before any class or method definitions in your file.
If you are working in an IDE like Eclipse, IntelliJ, or NetBeans, the editor can add this import for you automatically. When you type "Scanner" and the IDE recognizes it is missing, a red underline appears. Click the error or press a keyboard shortcut (often Ctrl+Shift+O in Eclipse or Alt+Enter in IntelliJ) and the IDE will insert the import statement.
Key Takeaways
- Place import java.util.Scanner; at the top of your Java file, before the class declaration.
- The Scanner class lives in the java.util package, so you must tell Java where to find it.
- Without the import statement, your code will not compile and the compiler will report an error.
- Most IDEs can add the import automatically when you use the Scanner class and request a fix.
- You only need to import Scanner once per file, no matter how many Scanner objects you create.
Where the Scanner class lives in Java's library
Java organizes its built-in classes into packages — folders that group related code. The Scanner class is stored in the java.util package, which contains utility classes for working with data structures, dates, and input. When you write import java.util.Scanner;, you are telling Java to look inside the java.util package and bring the Scanner class into your file so you can use it.
You could also write import java.util.*; to import everything in the java.util package at once. This works, but most programmers avoid it because it makes the code less clear — someone reading your file cannot when ready see which classes you are actually using. Importing Scanner by name is the standard practice.
What happens if you forget the import statement
If you try to create a Scanner object without importing it first, the compiler will stop and display an error like "cannot find symbol" or "Scanner is not defined". The program will not run until you add the import line.
Some beginners try to work around this by writing the full package path every time they use Scanner — for example, java.util.Scanner input = new java.util.Scanner(System.in); — instead of importing. This works, but it is tedious and makes your code harder to read. Always use the import statement instead.
The correct order for import statements in your file
Java expects a specific order at the top of your file: import statements come first, then the class declaration, then your methods and variables. If you have multiple imports, they can appear in any order relative to each other, but all imports must come before anything else.
A typical file structure looks like this:
import java.util.Scanner; import java.io.File; public class MyProgram { public static void main(String[] args) { Scanner input = new Scanner(System.in); } }
The import statements sit at the very top. The class declaration comes next. Inside the class, you can now use Scanner without any prefix.
Using Scanner after you import it
Once you have imported Scanner, you can create a Scanner object and use it to read input. The most common way is to read from the keyboard using System.in:
Scanner input = new Scanner(System.in);
You can also create a Scanner to read from a file by passing a File object instead of System.in. In either case, the import statement at the top of your file makes the Scanner class available to you.
Common mistakes when importing Scanner
A frequent error is typing the import statement inside a method or inside the class body instead of at the top of the file. Import statements must appear before the class declaration. If you put import java.util.Scanner; inside a method, the compiler will reject it.
Another mistake is misspelling the package name — for example, writing import java.util.scanner; (lowercase) or import java.utils.Scanner; (utils instead of util). Java is case-sensitive and the package names must match exactly. The correct spelling is java.util.Scanner with a lowercase u and t.
Some beginners also forget that they need to import Scanner every time they start a new Java file. The import statement is not global — it only applies to the file where you write it. Each file that uses Scanner must have its own import statement.
Frequently Asked Questions
Do I need to import Scanner if I am using an IDE?
Yes. The IDE can add the import for you automatically, but the import statement must still be in your file for the code to compile. The IDE is just saving you typing — it does not eliminate the need for the import.
Can I import Scanner and other classes from java.util in one line?
Yes. You can write import java.util.*; to import all classes in the java.util package. However, most programmers prefer to import Scanner by name because it makes the code clearer and avoids potential naming conflicts.
What if I have multiple Java files in my project?
Each file needs its own import statement if it uses Scanner. Importing Scanner in one file does not make it available in other files. You must add import java.util.Scanner; to every file that creates or uses a Scanner object.
Is there a difference between importing Scanner and creating a Scanner object?
Yes. The import statement tells Java where to find the Scanner class. Creating a Scanner object (like Scanner input = new Scanner(System.in);) actually makes a Scanner you can use. You need both — the import first, then the object creation.
What does the asterisk mean in import java.util.*?
The asterisk is a wildcard that means "import all classes in this package". Writing import java.util.*; brings in Scanner, ArrayList, HashMap, and every other class in java.util. It works, but naming Scanner specifically is clearer and is the standard approach.