Sub topic 1
Standard Input and `System.in`
Prerequisites
You should know basic Java syntax, including classes, methods, variables, and control flow. You should also be able to compile and run a Java program from the command line. This chapter assumes that you know the String, int, and double types and have basic familiarity with exceptions.
What standard input is
A running program often needs data from outside itself. For example, a program might receive a name typed by a user, numbers from a file, or text piped from another command.
Standard input, often called stdin, is the input stream that a program reads by default. In Java, standard input is represented by System.in. Its type is InputStream, which is a class for reading raw bytes.
System.in does not know that the incoming bytes represent names, numbers, or complete lines. An input-reading tool interprets those bytes. Scanner is one such tool.
flowchart TD
A[Keyboard or redirected input] --> B[Operating system]
B --> C[System.in]
C --> D[Input-reading tool]
D --> E[Java values]The keyboard is the usual source of standard input, but it is not the only source. A shell can redirect a file or pipe another command into the Java program.
Reading from `System.in`
The following program creates a Scanner that reads from standard input:
import java.util.Scanner;
public class Echo {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Type something: ");
String text = input.nextLine();
System.out.println("You typed: " + text);
}
}new Scanner(System.in) connects the scanner to the input stream. The call to nextLine() waits until a complete line is available. When the user presses Enter, the method returns the line as a String.
The prompt is written with System.out, which is standard output. Standard input and standard output are separate streams:
| Stream | Java expression | Usual purpose |
|---|---|---|
| Standard input | `System.in` | Read data |
| Standard output | `System.out` | Write normal results |
| Standard error | `System.err` | Write error messages |
A prompt is output, even though it controls when the user provides input.
Input can be redirected
Because System.in is a stream, the program does not need to know whether its data comes from a keyboard, a file, or another process. For example, suppose names.txt contains one line:
AdaYou can redirect that file into the program:
java Echo < names.txtThe program still calls nextLine() in exactly the same way. The shell connects the file to System.in before Java starts.
This separation is useful for testing. Instead of typing input manually, you can provide known input and compare the program's output with the expected result.
Input is not automatically typed
Everything arriving through standard input begins as characters or bytes. Java does not automatically turn 42 into the integer value 42. A reader must parse it.
Scanner can perform common conversions:
import java.util.Scanner;
public class ReadNumber {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Quantity: ");
int quantity = input.nextInt();
System.out.println("Quantity is " + quantity);
}
}If the user enters text that is not a valid integer, nextInt() cannot perform the conversion and reports an input error. Later chapters cover validation and recovery.
The lifetime of standard input
A program normally keeps System.in available for as long as it might need input. A scanner reads from the stream, but the scanner does not own the operating system's input source in isolation.
Closing a Scanner that wraps System.in also closes System.in. After that, another part of the program cannot read from standard input. For a small program that reads input once, this may not matter. For a larger program, close the input stream only when the entire program has finished using it.
Exercise
What does new Scanner(System.in) connect, and where does the input usually come from?
Show answer
It connects a Scanner to Java's standard input stream. The input usually comes from the user's keyboard, but it can also come from redirected files or another process.