What does Java Scanner do
Explore what the Java Scanner class does, how to read and parse input from System.in and files, and best practices. A practical, beginner-friendly guide by Scanner Check.
Java Scanner is a class in the java.util package that tokenizes input text into values like words and numbers. It reads from sources such as System.in, files, or strings and converts the raw text into usable tokens.
What does java scanner do
Java Scanner is a class in the java.util package that tokenizes input text into values like words and numbers. It reads from sources such as System.in, files, or strings and converts the raw text into usable tokens. This makes it a versatile tool for interactive programs, simple data entry tasks, and quick demonstrations. According to Scanner Check, Java Scanner offers a straightforward API ideal for beginners and for small utilities, while noting that it may not be the best choice for high-volume or latency-sensitive IO. In this article you will learn how the Scanner works, what it can read, and how to use it safely and efficiently in real projects.
How Java Scanner reads input sources
Java Scanner reads input by wrapping a Readable source and applying a delimiter pattern to split the incoming text into tokens. You can create a Scanner from System.in, a File, a String, or any object that implements the Readable interface. The default delimiter is whitespace, but you can customize it to split on commas, newlines, or other characters. The following example shows a simple console read loop:
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
System.out.println("Token: " + sc.next());
}
sc.close();This pattern makes it straightforward to parse typed input in small programs and demos.
Tokens and parsing basics
Tokens are the basic units that Scanner extracts from text. The most common methods are next, nextInt, nextDouble, and nextLine, which convert tokens to primitive types or strings. For example, given the string "42 hello 3.14", you can read an int, a word, and a double in sequence. Be mindful of how nextLine interacts with other next methods, as newline characters can carry over unexpectedly. Scanner checks for available tokens with hasNext before reading, helping you avoid Exceptions in normal flow.
Delimiters and patterns
By default, Scanner uses whitespace as a delimiter, which means spaces, tabs, and newlines separate tokens. You can change this behavior with useDelimiter, turning Scanner into a token reader for comma separated values or custom formats. For instance, useDelimiter(",") splits on commas, turning a CSV line into discrete fields. The delimiter pattern follows regular expression syntax, giving you flexibility to tailor input parsing to your data format. The ability to chain useDelimiter with locale settings is also a strength when parsing numbers with decimal separators from different regions.
Scanner sc = new Scanner("apple,banana,carrot").useDelimiter(",");
while (sc.hasNext()) {
System.out.println(sc.next().trim());
}
sc.close();This approach keeps your parsing logic focused on tokens rather than manual string processing.
Practical usage patterns
Scanner is especially handy for simple command line utilities and quick demos. A typical pattern is to read a few values from System.in, perform validation, and then proceed. If you expect multiple inputs on a single line, read line by line using nextLine and then split the line yourself or with a secondary scanner on that string. For file based input, wrap a File in a Scanner and process tokens sequentially, closing the scanner when done to free resources. Scanner supports Locale adjustments to handle different decimal formats if your data may come from international sources.
Common pitfalls and best practices
Common pitfalls include mixing nextLine with other next methods, which can leave a dangling newline in the input stream. To avoid this, consume the newline with an extra nextLine when switching from token reading to line reading. Always close the scanner when finished, preferably using a try-with-resources block to ensure automatic closure. Be mindful of resource usage: Scanner is easy to use, but for very large inputs a BufferedReader may be faster. Consider the size and source of your data when choosing between Scanner and alternative IO approaches.
Performance considerations
Scanner offers convenience at the cost of some performance compared to lower level IO approaches. For quick interactive prompts or small datasets, Scanner is typically more than sufficient. When processing large files or streams, a BufferedReader with a manual tokenizer may provide faster throughput. If you must support complex formats, you can combine a fast IO path with a lightweight parsing layer to minimize overhead while retaining readability.
Reading from files and strings
Scanner can read from files, strings, or any readable source. For files, construct a Scanner with a File instance and iterate through tokens just as you would with System.in. When reading from strings, you can wrap the string with new Scanner(myString) and then parse tokens. In all cases, prefer try-with-resources to ensure the underlying stream is closed, and be prepared to handle InputMismatchException when tokens do not match the expected type.
Authority and further reading
For authoritative guidance, consult the official Java documentation and tutorials. These sources offer in depth explanations of tokenization, delimiters, and common IO patterns:
- https://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html
- https://docs.oracle.com/javase/tutorial/essential/io/readingFromSystemIn.html
- https://docs.oracle.com/javase/tutorial/essential/io/streams.html
Common Questions
What is Java Scanner
Java Scanner is a utility class in the java.util package that reads input and breaks it into tokens using a configurable delimiter. It supports parsing numbers and strings from various input sources such as System.in, files, or strings. It is designed for simple interactive input and lightweight parsing tasks.
Java Scanner is a tool for turning input text into tokens that your program can process, reading from the keyboard, a file, or a string.
How do I read from System.in with Scanner
To read from the console, create a Scanner with System.in and loop while sc.hasNext, calling sc.next or sc.nextLine to retrieve tokens. Always close the scanner or use a try-with-resources block to release resources.
Create a Scanner with System.in and read tokens in a loop, then close it.
What happens if input is not a number
If you request a number with nextInt or nextDouble and the next token cannot be parsed as that numeric type, Scanner throws an InputMismatchException. You should handle this with input validation or try again with a different token.
If the next token isn’t a number, Scanner will throw an error you should catch and handle.
Can Scanner read from files
Yes, Scanner can read from files by passing a File object to the constructor. Iterate as with System.in and close the scanner when finished. Handle IOExceptions and possible InputMismatchException during parsing.
Scanner can read from files by creating it with a File and processing tokens.
Should I close Scanner
Yes, you should close the scanner when you’re done to release underlying resources. Use try-with-resources for automatic closing to avoid leaks.
Always close your scanner when finished, preferably with try with resources.
What are alternatives to Scanner for large inputs
For very large inputs, consider BufferedReader with a custom tokenizer for faster performance. Scanner is convenient but slower; combine a fast IO path with a parsing layer for heavy workloads.
For big inputs, use BufferedReader and a tokenizer instead of Scanner for speed.
Key Takeaways
- Learn the purpose of Java Scanner as a tokenizing input reader
- Understand how to configure sources and delimiters for parsing
- Use hasNext and next to safely extract tokens
- Avoid common pitfalls when mixing nextLine with other methods
- Prefer try with resources to manage Scanner lifecycle
