What the Scanner class does and why you need to import it

The Scanner class in Java reads input from the keyboard, files, or other sources so your program can respond to what a user types. It lives in the java.util package, which means you cannot use it without telling Java where to find it first. That instruction is called an import statement, and it goes at the very top of your code file before you write anything else.

When you write a program that asks a user for their name or age, Scanner is what actually captures those keystrokes and turns them into something your code can work with. Without importing it, Java will not recognize the word "Scanner" and your program will not run.

Key Takeaways

  • Import Scanner by writing import java.util.Scanner; at the top of your Java file, before any class declarations.
  • After importing, create a Scanner object with Scanner input = new Scanner(System.in); to read from the keyboard.
  • Use methods like nextLine() to read text, nextInt() to read whole numbers, and nextDouble() to read decimals.
  • Always close the Scanner when you are done with input.close(); to prevent resource leaks in your program.
  • Scanner reads one piece of input at a time, so if a user enters multiple words, next() stops at the first space while nextLine() reads the entire line.

The import statement: where it goes and what it does

The import statement must be the first line of code in your Java file, before you declare your class. Write it exactly as import java.util.Scanner; with the semicolon at the end. Java reads this line and loads the Scanner class from the java.util package so you can use it in your program.

If you have multiple imports, list them one per line, all before your class declaration. For example, if you also needed to import Random, you would write both import statements, then start your class. The order of imports does not matter, but they all come before anything else.

Some development environments like Eclipse or IntelliJ can add the import for you automatically. If you type "Scanner" and the editor underlines it in red, look for a suggestion to "import Scanner" or "add import" and click it. This saves you typing and prevents mistakes.

Creating a Scanner object to read keyboard input

After you import Scanner, you need to create an object — a working copy of the Scanner class that actually does the reading. Inside your main method or another method, write Scanner input = new Scanner(System.in); This line tells Java to create a new Scanner called "input" that listens to System.in, which is the keyboard.

You can name the Scanner object anything you want instead of "input" — "scan", "reader", or "keyboard" all work fine. The important part is new Scanner(System.in), which is the standard way to set up keyboard reading. Once this line runs, your Scanner is ready to use.

If you want to read from a file instead of the keyboard, you would write Scanner input = new Scanner(new File("filename.txt")); but this requires importing File as well. For most beginner programs, reading from the keyboard with System.in is what you need.

Reading different types of input with Scanner methods

Scanner has several methods, each designed for a different kind of input. nextLine() reads an entire line of text, including spaces, and stops when the user presses Enter. next() reads a single word and stops at the first space. nextInt() reads a whole number. nextDouble() reads a decimal number. nextBoolean() reads true or false.

Here is how they work in practice. If a user types "John Smith" and you call next(), you get only "John". If you call nextLine(), you get "John Smith". If a user types "25" and you call nextInt(), you get the number 25 as an integer your code can do math with, not as text.

Choose the method that matches what you expect the user to enter. If you ask "What is your name?" use nextLine() because names often have spaces. If you ask "How old are you?" use nextInt() because you need a number. Mixing them up is one of the most common mistakes — for example, using next() when you meant nextLine() will cut off part of the user's answer.

A complete example: putting it all together

Here is a short program that imports Scanner, creates it, reads input, and closes it properly:

import java.util.Scanner; public class HelloUser {   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("How old are you?");     int age = input.nextInt();          System.out.println("Hello, " + name + ". You are " + age + " years old.");          input.close();   } }

The import statement is at the top. Inside main, a Scanner is created. The program asks two questions, reads the answers with nextLine() and nextInt(), prints them back, and then closes the Scanner. When you run this program and type your name and age, it will greet you by name and repeat your age.

Closing the Scanner and avoiding resource leaks

After you finish reading input, always call input.close(); This tells Java to stop listening to the keyboard and release the resource. If you do not close it, Java keeps the connection open even after your program ends, which wastes system resources. In small practice programs this might not cause a visible problem, but it is a bad habit that causes real issues in larger applications.

The best place to close the Scanner is at the end of your main method or at the end of the method where you created it. If your program reads input in multiple places, you can create one Scanner at the start and close it at the very end, or create and close a Scanner in each method that needs it. Either way, make sure every Scanner you create gets closed.

Common mistakes and how to fix them

The most common mistake is forgetting the import statement. If you write Scanner without importing it first, you will see an error like "Scanner cannot be resolved to a type". The fix is straightforward: add import java.util.Scanner; at the top of your file.

Another frequent problem happens when you mix nextInt() and nextLine(). If you call nextInt() to read a number, then when ready call nextLine() to read text, the nextLine() will read an empty line instead of waiting for the user to type. This is because nextInt() leaves the Enter key press in the input buffer. The fix is to call input.nextLine(); once after nextInt() to clear that leftover Enter, then call nextLine() again to read the actual text.

A third issue is trying to read a number with nextInt() when the user types text instead. If you ask "How old are you?" and the user types "twenty-five" instead of "25", your program will crash with an error. To prevent this, you can check what the user typed before trying to convert it, but that requires more advanced techniques beyond the basic import and use of Scanner.

Frequently Asked Questions

Do I have to import Scanner or can I use java.util.Scanner without importing?

You can write the full name java.util.Scanner every time instead of importing, but that is tedious and unreadable. The import statement at the top lets you just write Scanner, which is the standard practice. Most Java code uses imports for this reason.

What is the difference between next() and nextLine()?

next() reads one word and stops at the first space. nextLine() reads the entire line, including spaces, and stops at Enter. Use next() for single words like a username, and nextLine() for full sentences or names with spaces.

Can Scanner read from a file instead of the keyboard?

Yes. Instead of new Scanner(System.in), write new Scanner(new File("myfile.txt")) This requires importing File from java.io as well. The methods like nextLine() and nextInt() work the same way whether reading from keyboard or file.

What happens if I do not close the Scanner?

Your program will still run and produce correct output, but Java keeps a resource open that should be released. In small programs this causes no visible harm, but in production code or long-running applications, unclosed Scanners can lead to resource exhaustion. It is a best practice to always close.

Can I create more than one Scanner in the same program?

Yes, you can create multiple Scanners if you need to read from different sources — one from the keyboard, one from a file, one from a network connection. Each one should be closed when you are done with it. For most beginner programs, one Scanner is enough.