To take user input in Java, create a Scanner object that reads from System.in, then call methods such as nextInt(), nextDouble() or nextLine(). The Scanner class lives in the java.util package, so you must import it. This is the standard way to read keyboard input in console programs while learning, and the scanner class java input pattern is the same in every beginner program.
This guide shows how to read numbers and text, how to avoid the well-known nextLine() problem, how to validate input and when to close the scanner.
System.in is the standard input stream (bytes from the keyboard). The Scanner wraps it and splits the text into tokens, converting them to the type you ask for.
| Method | Reads | Stops at |
|---|---|---|
next() | One word (token) | Whitespace |
nextLine() | A full line | The line break |
nextInt() | An int | Whitespace |
nextLong() | A long | Whitespace |
nextDouble() | A double | Whitespace |
nextBoolean() | true or false | Whitespace |
There is no nextChar(). To read one character, take the first character of a string: scanner.next().charAt(0).
Number parsing depends on the default locale. In some locales a double uses a comma as the decimal separator. If you need a fixed format, call scanner.useLocale(java.util.Locale.US).
This is the most common Scanner bug. nextInt() reads the number but leaves the Enter key (the line break) in the buffer. The next nextLine() then reads that empty remainder and returns an empty string at once.
Fix it by consuming the leftover line break first:
Another reliable approach is to read every value with nextLine() and convert it yourself with Integer.parseInt(scanner.nextLine().trim()).
If the user types text where a number is expected, nextInt() throws InputMismatchException. Use the hasNext... methods to check first.
The scanner.next() call in the failure branch is essential. Without it the bad token stays in the buffer and the loop runs forever. The methods hasNextInt(), hasNextDouble() and hasNextLine() also let you read until the input ends, which is useful when input is piped from a file:
Scanner splits on whitespace by default, so a line such as 10 20 can be read with two calls.
To use another separator, call scanner.useDelimiter(",").
Closing a Scanner that wraps System.in also closes System.in, and you cannot reopen it. In a small program create one Scanner, use it everywhere and close it at the end, or use try-with-resources:
Do not create a new Scanner on System.in inside a loop or in several methods, because each one may buffer input that the others then miss. Other options are BufferedReader for fast input in large datasets, and Console for passwords through System.console().readPassword().
nextInt() leaves the line break in the buffer, so the following nextLine() consumes only that. Add an extra scanner.nextLine() after the number to clear it.
Use scanner.next().charAt(0). Scanner has no nextChar() method.
Close it when you are finished with it, ideally with try-with-resources. Remember that closing a Scanner on System.in closes the standard input for the rest of the program.
It is fine for interactive programs, but slow for millions of numbers. Use BufferedReader with readLine() for competitive or bulk reading.
Import java.util.Scanner, create it with new Scanner(System.in) and call nextInt(), nextDouble(), next() or nextLine() to read typed input. Clear the leftover line break after number methods, validate with hasNextInt() before reading, create only one Scanner for System.in, and close it with try-with-resources when done.