Skip to content
Tutorials Center
My learning

Topics

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.

Java
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.

Java Reading numbers with Scanner

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.

Java Fast line reading with BufferedReader

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

Java
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 following nextLine() returns an empty string. Call nextLine() once to consume the rest of the line, or read everything with nextLine() and parse with Integer.parseInt.
  • NoSuchElementException. nextLine() and next() throw it when input has run out. Guard with hasNextLine() / hasNext().
  • InputMismatchException. Thrown by nextInt() when the next token is not an integer. The bad token stays in the buffer, so skip it with next() 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 Scanner closes System.in; later reads fail. In small programs, closing at the very end is fine.
  • Speed. Scanner uses regular expressions and is slow for hundreds of thousands of tokens; BufferedReader with StringTokenizer or split is much faster.
  • Locale. nextDouble() follows the default locale, so 3,5 may be accepted instead of 3.5. Use useLocale(Locale.ROOT) for predictable parsing.
  • Passwords: use Console.readPassword(), which does not echo; Scanner shows what is typed.
  • Encoding. InputStreamReader without a charset uses the default; name StandardCharsets.UTF_8 explicitly for portable code.