Skip to content

DevWorkbench

  • Home
  • Links
  • About
  • Toggle search form

Java Examples – Screen I/O

Posted on October 9, 2024October 9, 2024 By jeepdogjake

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:

  1. **Import Statement**: The `java.util.Scanner` class is imported to facilitate user input.
  2. **Scanner Object**: An instance of `Scanner` is created to read input from the standard input stream, `System.in`.
  3. **Prompting User**: The program prompts the user to enter a value using `System.out.print`.
  4. **Reading Input**: `scanner.nextLine()` is called to read the user’s input as a `String`.
  5. **Displaying Output**: The entered value is then printed to the screen with `System.out.println`.
  6. **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.

Programming Examples

Post navigation

Previous Post: How to reset Ctrl-End in Excel
Next Post: Java Examples – Date Parsing & Formatting

Related Posts

XSLT Example – Merging Two Documents Programming Examples
Java Examples – Date Parsing & Formatting Programming Examples
XSLT Example – “Joining” Different Nodes in One Document Programming Examples

Recent Posts

  • XSLT Example – “Joining” Different Nodes in One Document
  • XSLT Example – Merging Two Documents
  • Java Examples – Date Parsing & Formatting
  • Java Examples – Screen I/O
  • How to reset Ctrl-End in Excel

Copyright © 2026 DevWorkbench.

Powered by PressBook Masonry Dark