Java Reference ยท java.lang
Java System.in
Reference for System.in: reading standard input with Scanner, BufferedReader and raw bytes, end-of-input handling, token versus line pitfalls and Console.
System.in is the standard input stream: an InputStream connected to the keyboard, a piped file or the output of another program. It reads raw bytes, so you almost always wrap it in something friendlier: a Scanner for tokens and numbers, or a BufferedReader for fast line-by-line reading.
When the input is finished (the user pressed Ctrl+D or Ctrl+Z, or the pipe closed), reads report end-of-input rather than throwing.
Scanner in = new Scanner(System.in); String name = in.nextLine(); int age = in.nextInt();
Try it
The TryIt boxes have an Input field below the editor: type the lines there before pressing Run.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public static final InputStream in;
| Approach | Description |
|---|---|
new Scanner(System.in) |
Splits input into tokens. Methods: nextInt(), nextLong(), nextDouble(), next(), nextLine() and matching hasNext...() tests. |
new BufferedReader(new InputStreamReader(System.in)) |
readLine() returns the next line or null at end of input. Fast, but you parse numbers yourself. |
System.in.read() |
Reads one byte as 0..255, or -1 at end of input. Throws IOException. |
System.in.readAllBytes() |
Reads everything until the end (since Java 9). |
System.console() |
Returns a Console with readLine() and readPassword(). It was null when input or output was redirected; since Java 22 it can be non-null even then, so check console.isTerminal() as well as null. |
System.setIn(InputStream) |
Replaces the stream, useful in tests. |
Usage notes
- The newline trap.
nextInt()reads the number but leaves the line break in the buffer, so a followingnextLine()returns an empty string. CallnextLine()once to consume the rest of the line, or read everything withnextLine()and parse withInteger.parseInt. NoSuchElementException.nextLine()andnext()throw it when input has run out. Guard withhasNextLine()/hasNext().InputMismatchException. Thrown bynextInt()when the next token is not an integer. The bad token stays in the buffer, so skip it withnext()to avoid an endless loop.- Create one
Scanner. Several scanners over the same stream each buffer ahead, and the first one can swallow input the second needed. - Do not close casually. Closing a
ScannerclosesSystem.in; later reads fail. In small programs, closing at the very end is fine. - Speed.
Scanneruses regular expressions and is slow for hundreds of thousands of tokens;BufferedReaderwithStringTokenizerorsplitis much faster. - Locale.
nextDouble()follows the default locale, so3,5may be accepted instead of3.5. UseuseLocale(Locale.ROOT)for predictable parsing. - Passwords: use
Console.readPassword(), which does not echo;Scannershows what is typed. - Encoding.
InputStreamReaderwithout a charset uses the default; nameStandardCharsets.UTF_8explicitly for portable code.
Related
System,System.out,Integer.parseInt()- Lessons: User input, Output