Interactive Input and Output in Java जावा में इंटरैक्टिव इनपुट और आउटपुट

Interactive Input and Output in Java means taking input from the user at runtime and providing dynamic output based on the input.

जावा में इंटरैक्टिव इनपुट और आउटपुट का अर्थ है रनटाइम पर उपयोगकर्ता से इनपुट लेना और उसके आधार पर आउटपुट देना।


Key Points / मुख्य बिंदु:-

  1. Interactive Input: User enters data using console or GUI.
    इंटरैक्टिव इनपुट: उपयोगकर्ता कंसोल या GUI से डेटा देता है।

  2. Interactive Output: Program responds according to input.
    इंटरैक्टिव आउटपुट: प्रोग्राम इनपुट के अनुसार आउटपुट देता है।

  3. Common Classes: Scanner, BufferedReader, Console.
    सामान्य क्लासेस: Scanner, BufferedReader, Console


💻 Example 1: Using Scanner for Interactive I/O

import java.util.Scanner;

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

        System.out.print("Enter your name: ");
        String name = sc.nextLine();

        System.out.print("Enter your age: ");
        int age = sc.nextInt();

        System.out.println("Hello " + name + "! You are " + age + " years old.");
        sc.close();
    }
}

Sample Output:

Enter your name: Rahul
Enter your age: 22
Hello Rahul! You are 22 years old.

💻 Example 2: Using BufferedReader

import java.io.*;

public class InteractiveBufferedReader {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter your favorite language: ");
        String lang = br.readLine();

        System.out.println("You like " + lang + " programming!");
    }
}

Sample Output:

Enter your favorite language: Java
You like Java programming!

💻 Example 3: Using Console Class

import java.io.Console;

public class InteractiveConsole {
    public static void main(String[] args) {
        Console con = System.console();
        if (con != null) {
            String user = con.readLine("Enter username: ");
            char[] pass = con.readPassword("Enter password: ");

            System.out.println("Welcome " + user + "! Your password is securely read.");
        } else {
            System.out.println("Console not available.");
        }
    }
}

Sample Output (if console available):

Enter username: admin
Enter password: *****
Welcome admin! Your password is securely read.

🔹 Real-life Uses / वास्तविक उपयोग

  • Command-line Applications: User interacts with the program.
    कमांड-लाइन एप्लिकेशन: उपयोगकर्ता प्रोग्राम से संवाद करता है।

  • Login Systems: Taking username and password securely.
    लॉगिन सिस्टम: सुरक्षित रूप से यूज़रनेम और पासवर्ड लेना।

  • Interactive Games or Quizzes: Asking questions and displaying results.
    इंटरैक्टिव गेम्स या क्विज़: प्रश्न पूछना और परिणाम दिखाना।

Comments

Popular posts from this blog

What is a Web Browser? वेब ब्राउज़र क्या है?

Java's Support System जावा का सहयोगी तंत्र

The Internet and Java इंटरनेट और जावा का सम्बन्ध