Skip to main content

← Back to Overview

User Input and Display

Concept Explanation

Programs often need to interact with users. Taking user input allows a program to receive data from the user, making it dynamic and responsive. Displaying output is equally important for providing feedback, results, or prompts to the user. This concept is fundamental to almost all interactive applications.

Algorithm

  1. Start.
  2. Prompt the user to enter a value (e.g., name, number).
  3. Read the input provided by the user.
  4. Display the entered value back to the user.
  5. End.

Implementations

import java.util.Scanner; // Import the Scanner class

public class UserInputDisplay {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// --- Taking String Input ---
System.out.print("Enter your name: ");
String name = scanner.nextLine(); // Reads the entire line of input
System.out.println("Hello, " + name + "!");

// --- Taking Integer Input ---
System.out.print("Enter your favorite number: ");
int number = scanner.nextInt(); // Reads the next integer input
System.out.println("Your favorite number is: " + number);

scanner.close(); // It's good practice to close the scanner
}
}

Explanation

  • Java: Uses the Scanner class (java.util.Scanner) to read various types of input. nextLine() for strings and nextInt() for integers. scanner.close() is important for resource management.
  • Python: Uses the built-in input() function, which always reads input as a string. Explicit conversion functions like int() or float() are used to convert string input to numerical types when needed.
  • C: Employs fgets() for safe string input (reading an entire line) and scanf() for formatted input like integers. Proper format specifiers (%s, %d) and the address-of operator (&) are crucial with scanf(). \n added to printf for consistent output.
  • Oracle:
    • SQL: ACCEPT command (specific to SQL*Plus/SQL Developer) can prompt for user input into a substitution variable (&p_name), which can then be used in SQL queries.
    • PL/SQL: An anonymous PL/SQL block can also use substitution variables (&Enter_a_value). DBMS_OUTPUT.PUT_LINE is used to display output, which requires SET SERVEROUTPUT ON; to be enabled in the client environment.

Flowchart

Practice Problems

  • Modify the program to take two numbers as input and display their sum.
  • Take a single word as input and display it in uppercase.
  • Ask the user for their age and print a message like "You are [age] years old."

"The capacity to learn is a gift; the ability to learn is a skill; the willingness to learn is a choice." - Brian Herbert

📍 Visit Us

🏫 VD Computer Tuition Surat

VD Computer Tuition
📍 Address
2/66 Faram Street, Rustompura
Surat395002, Gujarat, India
📞 Phone / WhatsApp
+91 84604 41384
🌐 Website

Computer Classes & Tuition — Areas We Serve in Surat

AdajanAlthanAmroliAthwaAthwalinesBhagalBhatarBhestanCanal RoadChowkCitylightDumasGaurav PathGhod Dod RoadHaziraJahangirpuraKamrejKapodraKatargamLimbayatMagdallaMajura GateMota VarachhaNanpuraNew CitylightOlpadPalPandesaraParle PointPiplodPunaRanderRing RoadRustampuraSachinSalabatpuraSarthanaSosyo CircleUdhnaVarachhaVed RoadVesuVIP Road
📞 Call Sir💬 WhatsApp Sir