Scanner Class in Java – Reading Input from Users, Strings and Files
java.util.Scanner reads text input and breaks it into tokens that can be converted to numbers, booleans or strings. It is the easiest way to read keyboard input in console programs, and it can also read from strings, files and other streams.
This tutorial covers reading user input, the difference between next() and nextLine(), the famous newline trap, validating input with hasNextXxx(), custom delimiters and files, and when to use BufferedReader instead.
1. Reading Keyboard Input
Create a Scanner on System.in and call the method for the type you expect. The output below was produced with the sample input Asha Rao, 29 and 1.68, each typed on its own line:
import java.util.Scanner;
public class ReadUserInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter your name: ");
String name = sc.nextLine(); // whole line
System.out.print("Enter your age: ");
int age = sc.nextInt(); // a token parsed as int
System.out.print("Enter your height in metres: ");
double height = sc.nextDouble();
System.out.println();
System.out.println("Hello " + name + ", you are " + age + " and " + height + " m tall.");
// Do not close a Scanner wrapping System.in if other code still needs to read from it
}
}
Enter your name: Enter your age: Enter your height in metres: Hello Asha Rao, you are 29 and 1.68 m tall.
| Method | Reads |
|---|---|
next() | The next token (up to whitespace) |
nextLine() | The rest of the current line, without the line separator |
nextInt(), nextLong(), nextDouble(), nextBoolean()… | A token parsed as that type |
hasNext(), hasNextInt(), hasNextLine()… | Whether such input is available, without consuming it |
useDelimiter(pattern) | Change the token separator (default: whitespace) |
close() | Close the scanner and its source |
2. The nextInt() + nextLine() Trap
nextInt() reads the number but leaves the newline after it. A following nextLine() then returns that empty remainder immediately. Sample input: 3, Wireless Mouse, 5, USB Keyboard:
import java.util.Scanner;
public class NextLineTrap {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Quantity: ");
int qty = sc.nextInt(); // leaves the newline in the buffer
System.out.print("Product (buggy): ");
String product = sc.nextLine(); // reads that leftover empty line!
System.out.println();
System.out.println("qty=" + qty + ", product='" + product + "'");
System.out.println("(the product name is still waiting: '" + sc.nextLine() + "')");
System.out.print("Quantity: ");
qty = sc.nextInt();
sc.nextLine(); // FIX: consume the rest of the line
System.out.print("Product (fixed): ");
product = sc.nextLine();
System.out.println();
System.out.println("qty=" + qty + ", product='" + product + "'");
}
}
Quantity: Product (buggy): qty=3, product='' (the product name is still waiting: 'Wireless Mouse') Quantity: Product (fixed): qty=5, product='USB Keyboard'
The fix is to call sc.nextLine() once after reading a number, or to read every line with nextLine() and parse it with Integer.parseInt(line.trim()).
3. Validating Input
If the input does not match the expected type, nextInt() throws InputMismatchException. Check with hasNextInt() first. Sample input: 78 91 abc, 150 66, done:
import java.util.Scanner;
public class ValidateInput {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int total = 0, count = 0;
System.out.println("Enter marks (type 'done' to finish):");
while (sc.hasNext()) {
if (sc.hasNextInt()) { // check before reading
int m = sc.nextInt();
if (m < 0 || m > 100) { System.out.println(" ignored out-of-range value " + m); continue; }
total += m; count++;
} else {
String word = sc.next();
if (word.equalsIgnoreCase("done")) break;
System.out.println(" '" + word + "' is not a number");
}
}
System.out.println("count = " + count + ", average = " + (count == 0 ? 0 : total / (double) count));
}
}
Enter marks (type 'done' to finish): 'abc' is not a number ignored out-of-range value 150 count = 3, average = 78.33333333333333
4. Strings, Delimiters and Files
A scanner can read any text source and split on any regular expression:
import java.io.IOException;
import java.nio.file.*;
import java.util.Scanner;
public class ScanStringsAndFiles {
public static void main(String[] args) throws IOException {
// Scanning a String with a custom delimiter
try (Scanner sc = new Scanner("Asha;29;Pune").useDelimiter(";")) {
System.out.println("name=" + sc.next() + ", age=" + sc.nextInt() + ", city=" + sc.next());
}
// Scanning a file line by line
Path file = Path.of(System.getProperty("java.io.tmpdir"), "orders.txt");
Files.writeString(file, "A-17,2499\nB-42,799\nC-99,1299\n");
double sum = 0;
try (Scanner sc = new Scanner(file)) {
while (sc.hasNextLine()) {
String[] parts = sc.nextLine().split(",");
sum += Double.parseDouble(parts[1]);
}
}
System.out.println("order total = " + sum);
Files.delete(file);
}
}
name=Asha, age=29, city=Pune order total = 4597.0
Use try-with-resources to close scanners on files and strings. Avoid closing a scanner on System.in while the program still needs keyboard input, because closing it also closes System.in.
5. Scanner vs BufferedReader
| Aspect | Scanner | BufferedReader |
|---|---|---|
| Parsing | Built-in (nextInt, nextDouble) | Manual (Integer.parseInt) |
| Speed | Slower (regex-based) | Much faster for large input |
| Buffer | 1 KB | 8 KB by default |
| Exceptions | Unchecked (InputMismatchException) | Checked IOException |
| Thread-safe | No | Yes |
| Best for | Interactive console programs, small inputs | Large files, competitive programming |
6. Summary
Scannerreads tokens fromSystem.in, strings, files and streams and parses them into types.next()reads a token;nextLine()reads the rest of the line.- After
nextInt()and similar methods, consume the leftover newline before callingnextLine(). - Use
hasNextXxx()to validate input and avoidInputMismatchException. - Use
BufferedReaderfor large or performance-sensitive input.
Related Tutorials
Frequently Asked Questions (FAQ)
Q1. What is the Scanner class in Java?
A class in java.util that reads text from a source and splits it into tokens that can be parsed as primitive types or strings.
Q2. How do I take user input in Java?
Create Scanner sc = new Scanner(System.in); and call methods such as nextLine(), nextInt() or nextDouble().
Q3. What is the difference between next() and nextLine()?
next() reads one word (up to whitespace). nextLine() reads everything up to the end of the line.
Q4. Why does nextLine() skip input after nextInt()?
nextInt() leaves the newline character in the buffer, and nextLine() reads that empty remainder. Call nextLine() once to consume it.
Q5. What is InputMismatchException?
The exception thrown when the next token does not match the requested type, for example letters when calling nextInt().
Q6. How do I check input before reading it?
Use hasNextInt(), hasNextDouble() and similar methods, which do not consume input.
Q7. Should I close a Scanner on System.in?
Only when you are completely finished with keyboard input. Closing it also closes System.in, which cannot be reopened.
Q8. How do I read a char with Scanner?
There is no nextChar(). Use sc.next().charAt(0).
Q9. How do I read a whole file with Scanner?
Use new Scanner(path) and loop with hasNextLine()/nextLine(). For whole files, Files.readAllLines or Files.readString is simpler.
Q10. What is the default delimiter?
Whitespace (spaces, tabs and line breaks). Change it with useDelimiter.
Q11. Is Scanner thread-safe?
No. Use one scanner per thread or synchronize access.
Q12. Why is Scanner slow for large input?
It uses regular expressions to find and parse tokens. BufferedReader with manual parsing is much faster.
Q13. Does nextInt() depend on locale?
Yes. Number formats, such as decimal and grouping separators, follow the scanner's locale; set it with useLocale(Locale.US) if needed.
Q14. How do I read multiple numbers on one line?
Call nextInt() repeatedly; whitespace between numbers is skipped automatically.
Q15. What happens when input ends?
hasNext() returns false, and calling next() throws NoSuchElementException.
Q16. What is the difference between Scanner and BufferedReader?
Scanner parses tokens and types for convenience; BufferedReader reads lines efficiently and leaves parsing to you.
Q17. Can Scanner read from a String?
Yes: new Scanner("10 20 30"), which is useful for parsing and testing.
Q18. What is Console as an alternative?
System.console() provides readLine and readPassword (which hides input), but returns null when no console is available, such as in IDEs.
Q19. Which package contains Scanner?
java.util; import it with import java.util.Scanner;.
Q20. Since which version is Scanner available?
Java 5.