Declaring a Scanner starts with importing the class from Java's standard library
To use a Scanner in Java, you must first tell your program where to find it. Java keeps Scanner in a package called java.util, which is a collection of pre-built tools. Before you write any Scanner code, you add an import statement at the very top of your file, above your class definition. This single line tells Java to load the Scanner class so you can use it.
The import statement you need is:
import java.util.Scanner;
That is all that stands between your code and a working Scanner. Without this line, Java will not recognize the word "Scanner" and your program will not compile.
Key Takeaways
- Place import java.util.Scanner; at the top of your Java file, before your class declaration.
- After importing, create a Scanner object by writing Scanner scan = new Scanner(System.in); to read from the keyboard.
- You can also pass a file or other input source to Scanner instead of System.in if you need to read from somewhere else.
- Always close your Scanner when you are done with it by calling scan.close(); to free up system resources.
Where the import statement goes in your file
The import must sit at the very beginning of your Java file, before anything else except comments. If your file has a package declaration (a line that says package yourpackagename;), the import comes after that but before your class. Here is the correct order:
First: package declaration (if you have one) Second: import statements Third: your class definition and code
Java reads your file from top to bottom and needs to know where Scanner lives before it encounters the word "Scanner" in your code. If you put the import in the middle of your class or at the bottom, Java will throw a compilation error.
Creating a Scanner object after you import it
Importing Scanner is only the first step. After the import, you still need to create an actual Scanner object that your code can use. You do this inside a method (usually main) by writing a declaration statement.
The most common way is to read from the keyboard:
Scanner scan = new Scanner(System.in);
This line creates a new Scanner called scan and tells it to listen to System.in, which is the keyboard. You can name your Scanner anything you want — scan, input, reader — but the pattern is always the same: new Scanner() with a source inside the parentheses.
Reading input with your Scanner
Once you have declared and created your Scanner, you can use it to read what the user types. Scanner has several methods for different types of input:
scan.nextLine() reads an entire line of text, including spaces. scan.nextInt() reads the next whole number. scan.nextDouble() reads the next decimal number. scan.next() reads the next word (stops at whitespace).
For example, if you want to store what the user types into a variable, you would write:
String userName = scan.nextLine();
This asks the user to type something, waits for them to press Enter, and stores their answer in the variable userName.
Closing your Scanner when you are finished
After you have read all the input you need, you should close your Scanner by calling the close() method. This tells Java to release the resources your Scanner was using, which is good practice even in small programs:
scan.close();
If you do not close your Scanner, Java will eventually do it for you when your program ends, but closing it yourself is cleaner and prevents resource leaks in larger applications. Think of it like closing a door after you walk through — it is not strictly necessary, but it is the right habit to build.
Declaring a Scanner for a file instead of the keyboard
Sometimes you need to read from a file instead of waiting for keyboard input. The import statement stays the same, but the Scanner declaration changes. You also need to import the File class:
import java.util.Scanner; import java.io.File;
Then create your Scanner like this:
Scanner scan = new Scanner(new File("myfile.txt"));
This tells Scanner to read from a file called myfile.txt in your project folder. The rest of the code works the same way — you use nextLine(), nextInt(), and the other methods just as you would with keyboard input. When you are done, close the Scanner the same way: scan.close();
A complete example from start to finish
Here is what a working program looks like with Scanner imported and declared:
import java.util.Scanner; public class MyProgram { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.println("What is your name?"); String name = scan.nextLine(); System.out.println("Hello, " + name); scan.close(); } }
The import sits at the top. The Scanner is created inside the main method. The program reads one line of input, prints it back, and closes the Scanner. This is the pattern you will use in almost every program that needs keyboard input.
Frequently Asked Questions
What happens if I forget the import statement?
Java will not recognize the word "Scanner" and will show a compilation error that says something like "cannot find symbol: class Scanner". The program will not run. Adding the import line at the top fixes it when ready.
Can I import Scanner without importing the whole java.util package?
Yes. You can write import java.util.*; to import everything in java.util at once, but it is better practice to import only Scanner by name. This keeps your code clear about what you are using and avoids accidentally importing things you do not need.
Do I need to close the Scanner if my program is about to end anyway?
Technically no — Java will close it when the program exits. But closing it yourself is still good practice. In larger programs or programs that run for a long time, leaving Scanners open can cause problems. Building the habit now saves you trouble later.
Can I create more than one Scanner in the same program?
Yes. You can create multiple Scanners, each reading from a different source. For example, one Scanner could read from the keyboard and another from a file. Just give each one a different name and close each one when you are done.
What is the difference between System.in and a File?
System.in is the keyboard — it waits for the user to type. A File is stored data on your computer that the Scanner reads line by line. Both work with Scanner the same way, but System.in is interactive and a File is static.