Scanner reads input from the keyboard, files, and other sources so your Java program can work with data a user types or a file contains
The Scanner class in Java is a tool that lets your program take in information from outside — usually from a keyboard, a text file, or a network connection. Without Scanner, your Java program would have no way to ask a user for their name, read numbers from a file, or process text that comes from anywhere but the code itself. Scanner does the work of grabbing that input, breaking it into pieces, and handing it to your program in a form Java can use.
Scanner is part of the java.util package, which means you have to import it before you can use it. The class handles the messy details of reading raw data — it knows how to find where one piece of data ends and another begins, and it can convert text into numbers, whole words, or full lines depending on what your program needs.
Key Takeaways
- Scanner reads input from a keyboard, file, or other source and breaks it into usable pieces for your program.
- You create a Scanner object by pointing it at a source — System.in for keyboard input or a File object for file input.
- Common Scanner methods include nextLine() for a full line of text, nextInt() for a whole number, and next() for a single word.
- Scanner reads data in the order it appears and stops when it runs out, so you need to check whether more data exists before reading.
- You should close a Scanner when you are done with it to free up system resources, especially when reading from files.
How to create a Scanner and point it at a source
To use Scanner, you first write an import statement at the top of your Java file: import java.util.Scanner; Then you create a Scanner object and tell it where to read from. The most common source is the keyboard, which in Java is called System.in. The code looks like this:
Scanner input = new Scanner(System.in);
This line creates a Scanner named input that listens to whatever the user types. If you want to read from a file instead, you point Scanner at a File object. For example, Scanner fileReader = new Scanner(new File("data.txt")); tells Scanner to read from a file named data.txt in your project folder. Either way, Scanner is now ready to pull data from that source.
The most common Scanner methods and what they do
Scanner has several methods that each grab data in a different way. nextLine() reads everything the user types until they press Enter — it captures the whole line, including spaces. next() reads only the next word (it stops at whitespace like spaces or line breaks). nextInt() reads the next whole number and converts it from text into an integer your program can do math with. nextDouble() does the same for decimal numbers.
Here is a real example: if a user types "John 25", then next() returns "John" and the next next() returns "25" as text. But nextInt() would return 25 as a number you can add or subtract. nextLine() would return the entire "John 25" as one string.
Scanner also has hasNext(), hasNextInt(), and hasNextLine() — these check whether more data is waiting before you try to read it. This matters because if you call nextInt() and no number is there, your program crashes. Checking with hasNextInt() first prevents that.
Reading from a file versus reading from the keyboard
Reading from the keyboard and reading from a file use the same Scanner methods, but you set them up differently. For keyboard input, you use System.in as shown above. For a file, you create a File object first, then pass it to Scanner. The File object needs to be imported from java.io, so your imports look like: import java.util.Scanner; import java.io.File;
When you read from a file, Scanner goes through it line by line or word by word in order. Once it reaches the end, it stops — there is no more data to read. When you read from the keyboard, Scanner waits for the user to type something and press Enter. The user can keep typing as long as they want, and Scanner keeps reading until the program tells it to stop or the user closes the input stream (usually by pressing Ctrl+D on Mac/Linux or Ctrl+Z on Windows).
One important difference: when you are done reading from a file, you should call close() on your Scanner to release the file so other programs can use it. With keyboard input, closing Scanner is less critical, but it is still good practice.
Why Scanner breaks input into pieces automatically
When a user types "apple banana cherry" and presses Enter, that is one long string of text to the computer. Scanner's job is to break it into pieces based on what you ask for. By default, Scanner uses whitespace (spaces, tabs, line breaks) as the divider. So next() three times in a row would give you "apple", then "banana", then "cherry".
This automatic splitting is why Scanner is useful — you do not have to write code to find where one word ends and another begins. Scanner handles it. You can even change the divider (called a delimiter) if you need to split on something other than whitespace, like commas in a CSV file, but the default whitespace splitting covers most everyday tasks.
Common mistakes and how to avoid them
The most common mistake is calling nextInt() or nextDouble() when the user typed text instead of a number. Your program will crash with an exception. Always check with hasNextInt() or hasNextDouble() first, or wrap your Scanner call in a try-catch block to handle the error gracefully.
Another mistake is mixing nextLine() with next() or nextInt(). If you call nextInt() and the user types "25" and presses Enter, Scanner reads the 25 but leaves the line break sitting there. The next nextLine() will read that empty line instead of waiting for new input. To avoid this, call nextLine() after nextInt() to consume the leftover line break: int age = input.nextInt(); input.nextLine();
Forgetting to close Scanner when reading from files wastes system resources and can prevent other parts of your program from accessing the file. Make it a habit to call input.close(); when you are finished reading.
When to use Scanner versus other input methods
Scanner is the standard choice for reading text-based input in most Java programs, especially for learning and for small projects. It is straightforward, readable, and handles the common cases well. For very large files or high-performance applications, other methods like BufferedReader or NIO (New Input/Output) can be faster, but Scanner is almost always the right starting point.
If your program needs to read structured data like JSON or XML, you would use a specialized library instead of Scanner. But for reading a user's name, a list of numbers from a file, or lines of text, Scanner is the tool Java programmers reach for first.
Frequently Asked Questions
What happens if I try to read an integer but the user types a word?
Your program will throw an exception and crash unless you check with hasNextInt() first or catch the exception in a try-catch block. Always validate that the data type you expect is actually there before reading it.
Do I have to close Scanner when I am done?
For keyboard input (System.in), closing is not strictly required, but it is good practice. For file input, you should always close Scanner to release the file and free up system resources. Use input.close(); when you are finished.
Can Scanner read from a network connection or a URL?
Yes. You can pass Scanner a URL connection or an InputStream from a network socket, not just System.in or a File. The methods work the same way — Scanner still breaks the incoming data into lines, words, or numbers depending on which method you call.
Why does nextLine() sometimes return an empty string?
This usually happens when you call nextInt() or next() and then when ready call nextLine(). The first method leaves the line break behind, and nextLine() reads that empty line. Call nextLine() once after nextInt() to consume the leftover line break.
Can I use Scanner to read from a string instead of a file or keyboard?
Yes. You can create a Scanner from a String object: Scanner scan = new Scanner("apple banana cherry"); This is useful for testing or parsing a string that is already in memory, though it is less common than reading from files or user input.