Screen I/O in Java is generally done using the `Scanner` class which is a part of the `java.util` package. Here’s a simple Java program example:
import java.util.Scanner;
public class UserInputExample {
public static void main(String[] args) {
// Create a Scanner object to read input
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a value
System.out.print("Enter a value: ");
// Read the value entered by the user
String userInput = scanner.nextLine();
// Output the value to the screen
System.out.println("You entered: " + userInput);
// Close the scanner object
scanner.close();
}
}
An explanation of the example:
- **Import Statement**: The `java.util.Scanner` class is imported to facilitate user input.
- **Scanner Object**: An instance of `Scanner` is created to read input from the standard input stream, `System.in`.
- **Prompting User**: The program prompts the user to enter a value using `System.out.print`.
- **Reading Input**: `scanner.nextLine()` is called to read the user’s input as a `String`.
- **Displaying Output**: The entered value is then printed to the screen with `System.out.println`.
- **Closing Scanner**: It’s good practice to close the `Scanner` object with `scanner.close()` to free up system resources.
Compile and run this program in a Java environment, and it will wait for the user to input a value, and then display the entered value back to the console.