Declaring a Scanner in Java: The Basic Syntax
To declare a Scanner in Java, you import the Scanner class from the java.util package, then create a new Scanner object and assign it to a variable. The most common declaration looks like this:
import java.util.Scanner; goes at the top of your file, before your class definition. Then inside your method (usually main), you write: Scanner scanner = new Scanner(System.in); This creates a Scanner object named scanner that reads from the keyboard.
The variable name (scanner in this example) can be anything you choose, but naming it clearly helps you remember what it does. Some developers use input, reader, or scan instead. The important part is that System.in tells the Scanner to listen to keyboard input.
Key Takeaways
- Import the Scanner class with import java.util.Scanner; at the very top of your file, before any class declaration.
- Declare a Scanner variable using Scanner variableName = new Scanner(System.in); inside the method where you need it.
- System.in means the Scanner will read from keyboard input; you can also pass a File object to read from a file instead.
- Close your Scanner with scanner.close(); when you are finished to free up system resources.
- You can declare multiple Scanner objects in the same program if you need to read from different sources.
Where to Place the Import Statement
The import statement must appear at the very beginning of your Java file, before the class declaration. If your file has multiple import statements, they all go together at the top. Java reads imports first, so the Scanner class is available everywhere in that file.
If you forget the import statement, the compiler will throw an error saying "cannot find symbol" when you try to use Scanner. Adding the import fixes it when ready. Most Java editors (like Eclipse or IntelliJ) will suggest the import automatically when you type Scanner.
Declaring Scanner Inside Methods vs. as a Class Variable
Most of the time, you declare a Scanner inside the method where you use it—usually inside main(). This keeps the Scanner local to that method and makes the code easier to follow. The declaration Scanner scanner = new Scanner(System.in); inside main() is the standard approach for straightforward programs.
If you need to use the same Scanner in multiple methods, you can declare it as a class variable instead. This means declaring it inside the class but outside any method, at the top with other class variables. Class-level Scanners stay open for the entire program, which is useful for programs that read input repeatedly, but requires more careful management.
Reading Different Types of Input
Once you have declared your Scanner, you use different methods to read different data types. scanner.nextLine() reads an entire line of text as a String. scanner.nextInt() reads the next integer. scanner.nextDouble() reads a decimal number. scanner.next() reads a single word (stops at whitespace).
Choosing the right method matters. If a user types "25 apples" and you call nextInt(), the Scanner reads 25 and leaves " apples" in the buffer. The next call to nextLine() will read only the leftover space and text, not a fresh line. This is a common source of confusion when mixing input methods.
Closing Your Scanner to Avoid Resource Leaks
After you finish reading input, call scanner.close(); to close the Scanner. This tells Java to release the system resources the Scanner was using. If you declare a Scanner but never close it, Java will eventually close it when the program ends, but closing it explicitly is better practice.
In longer programs or programs that create many Scanners, forgetting to close them can cause resource leaks. A straightforward program with one Scanner in main() will work fine either way, but building the habit of closing resources now makes your code more professional and prevents problems later.
Declaring a Scanner to Read from a File
You can also declare a Scanner to read from a file instead of the keyboard. The syntax is slightly different: Scanner scanner = new Scanner(new File("filename.txt")); This requires an additional import: import java.io.File; at the top of your file.
When reading from a file, the Scanner works the same way—you still use nextLine(), nextInt(), and other methods. The difference is that the Scanner pulls data from the file instead of waiting for keyboard input. If the file does not exist, Java throws a FileNotFoundException, which you must handle with a try-catch block or declare in your method signature.
Common Mistakes When Declaring Scanners
Forgetting the import statement is the most common error. Without import java.util.Scanner;, your code will not compile. The second mistake is declaring a Scanner inside a method and then trying to use it in another method—the variable only exists inside the method where it was declared.
A third mistake is mixing nextInt() and nextLine(). When you call nextInt(), it reads the number but leaves the newline character in the input buffer. The next nextLine() call reads that leftover newline as an empty string. To avoid this, call nextLine() after nextInt() to clear the buffer, or use nextLine() for everything and parse integers manually with Integer.parseInt().
Frequently Asked Questions
Do I have to name my Scanner "scanner"?
No. You can name it anything that follows Java naming rules (starts with a letter, no spaces, no special characters except underscore). Common alternatives are input, reader, or scan. Choose a name that makes sense in your program.
What happens if I declare a Scanner but never use it?
The program will compile and run without error, but you are wasting a system resource. Java will close it when the program ends, but it is better practice to either use the Scanner or not declare it at all.
Can I declare two Scanners in the same program?
Yes. You might declare one to read from the keyboard and another to read from a file, or two separate file Scanners. Each one is a separate object with its own variable name. Just remember to close each one when you are finished.
What does System.in mean?
System.in is Java's standard input stream—the keyboard by default. It is a built-in object that represents where input comes from. You pass it to the Scanner constructor to tell the Scanner to listen to keyboard input instead of a file or network connection.
Do I need to close a Scanner that reads from System.in?
Technically, closing System.in closes the entire input stream for your program, which can cause problems if you declare another Scanner later. For keyboard input, many developers skip the close() call. For file Scanners, always close them to free up file handles.