Java Input & Output (Scanner Explained With Examples)
Java Input & Output (Scanner Explained With Examples)
To build interactive Java programs, we must take input from users.
✅ Input in Java (Scanner Class)
We use Scanner to read input:
import java.util.Scanner;
public class Main {
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("Welcome " + name + "! You are " + age + " years old.");
}
}
✅ Output in Java
We use:
-
System.out.print()— prints text -
System.out.println()— prints text + new line
🎯 Summary
Feature |
Method |
|---|---|
Input |
Scanner class |
Display output |
print / println |
Learn with Funny Story 😅:
😂 The Story of Ramu and the Scanner
Ramu wanted to learn Java.
But there was one big problem:
👉 Java wanted input… and Ramu wanted output!
So one day, Ramu sat in front of his laptop and asked:
“Java bhai… how do I take input from the user?”
Java replied:
“Call my friend… Scanner!”
So Ramu wrote:
Scanner sc = new Scanner(System.in);
Suddenly a tiny character popped out of the laptop wearing glasses.
Scanner: “Hello! Tell me what you want me to read.”
Ramu: “Bro, just read whatever users type!”
Scanner: “Okay! Just call me with nextLine().”
Ramu typed something…
Scanner grabbed it…
Java printed it…
System.out.println("You entered: " + sc.nextLine());
Ramu jumped and shouted:
“AREY! This is magic!”
Scanner adjusted his tiny glasses and said:
“Magic? No no… I just scan things.”
Ramu nodded proudly.
Java smiled silently.
And the output appeared perfectly.
Keep practicing — coding gets easier every day 💪🔥
More Topics :
What is Java? Complete Beginner Friendly Guide (2025)
Why Java is the Best Programming Language for Beginners (2025 Guide)

Comments
Post a Comment