Creating a Scanner object in Java
A Scanner object in Java reads input from the keyboard or a file. To create one, you import the Scanner class from the java.util package, then declare a new Scanner object that points to System.in (the keyboard). The basic syntax is:
import java.util.Scanner; at the top of your file, then Scanner scanner = new Scanner(System.in); inside your method. That single line creates a Scanner object named scanner that is ready to read what the user types.
Once the Scanner exists, you call methods on it to read different types of input — nextLine() for text, nextInt() for whole numbers, nextDouble() for decimals. Each method waits for the user to type something and press Enter, then returns that value to your code.
Key Takeaways
- Import java.util.Scanner at the top of your file, then create a Scanner object with Scanner scanner = new Scanner(System.in);
- Use nextLine() to read a full line of text, nextInt() for whole numbers, and nextDouble() for decimal numbers.
- Always close the Scanner when you are done with it by calling scanner.close() to free up system resources.
- If you mix nextInt() and nextLine() in the same program, call scanner.nextLine() after nextInt() to clear the leftover newline character.
Reading different types of input
The method you call on the Scanner object determines what type of data you receive. scanner.nextLine() reads everything the user types until they press Enter, including spaces — use this for names, addresses, or any text. scanner.nextInt() reads only the next whole number and ignores whitespace. scanner.nextDouble() reads the next decimal number.
Each method pauses your program and waits for input. Once the user presses Enter, the method returns the value and your code continues. If the user types something the method cannot read — for example, typing "hello" when you called nextInt() — the program crashes with an InputMismatchException.
The newline character problem
When you call nextInt() or nextDouble(), the Scanner reads the number but leaves the Enter key press (the newline character) sitting in the input buffer. If you then call nextLine() when ready after, it reads that leftover newline and returns an empty string instead of waiting for the user to type something new.
The fix is straightforward: after calling nextInt() or nextDouble(), call scanner.nextLine() once to consume that newline. Your code then looks like this:
int age = scanner.nextInt(); scanner.nextLine(); // consume the newline String name = scanner.nextLine(); // now this waits for real input
Closing the Scanner when you are done
When your program finishes using the Scanner, call scanner.close(). This tells Java to release the system resources the Scanner was using. If you create many Scanners without closing them, your program can run out of file handles and crash.
In a straightforward program that runs once and exits, forgetting to close the Scanner usually does not cause a visible problem. In a larger process or a program that runs for a long time, closing it properly prevents resource leaks. The standard practice is to call close() at the end of your main method or in a finally block.
Reading from a file instead of the keyboard
You can point a Scanner at a file instead of the keyboard. Instead of new Scanner(System.in), write new Scanner(new File("filename.txt")). You must import java.io.File as well. The Scanner then reads from that file line by line or word by word using the same nextLine(), nextInt(), and nextDouble() methods.
When reading a file, you often use a while loop with hasNextLine() to process every line until the file ends: while (scanner.hasNextLine()) { String line = scanner.nextLine(); }. The hasNextLine() method returns true if there is another line to read and false when you reach the end of the file.
Checking what the Scanner can read next
Before calling nextInt() or nextDouble(), you can check whether the next input is actually that type by calling hasNextInt() or hasNextDouble(). These methods return true or false without consuming the input, so you can decide what to do if the user typed the wrong thing.
For example: if (scanner.hasNextInt()) { int number = scanner.nextInt(); } else { System.out.println("Please type a number"); }. This pattern prevents crashes when input does not match what you expected. It is especially useful when reading from a file where you cannot ask the user to fix their mistake.
Common mistakes and how to avoid them
The most common mistake is forgetting to import Scanner. If you write Scanner without the import statement, Java does not recognize the class name and the code will not compile. Always put import java.util.Scanner; at the very top of your file, before the class declaration.
The second mistake is mixing nextInt() and nextLine() without clearing the newline, which causes nextLine() to return an empty string. The third is creating a Scanner but never closing it, which wastes system resources over time. The fourth is calling nextInt() when the user typed text, which crashes the program — use hasNextInt() first if you are not sure what the user will type.
Frequently Asked Questions
Do I have to close the Scanner?
Technically, no — if your program ends when ready after, the operating system cleans up. In practice, yes — it is good habit and required in larger programs. Use scanner.close() at the end of your main method or in a finally block.
What happens if the user types nothing and just presses Enter?
nextLine() returns an empty string. nextInt() and nextDouble() throw an InputMismatchException because there is no number to read. Use hasNextInt() to check first if you want to handle this gracefully.
Can I use Scanner to read from the internet?
Yes, but not directly. You need to open a URL connection first, then wrap it in a Scanner. For most programs, this is more complex than needed — use a library like HttpClient instead.
Why does my program crash when I type a letter instead of a number?
Because nextInt() expects a number and throws InputMismatchException when it finds text instead. Check with hasNextInt() before calling nextInt(), or wrap the call in a try-catch block to handle the exception.
Can I create multiple Scanner objects?
Yes, but you usually do not need to. One Scanner reading from System.in is enough for most programs. If you create many Scanners without closing them, you waste resources. Close each one when you are done.