Skip to main content

User Input and Display ⌨️

Java User Input & Display is a core Java concept covering learn how to take user input in Java using the Scanner class. Master reading Strings, Integers, and Decimals from the console. This topic is essential for academic learning, board exam preparation, and developing optimized real-world code.

Mentor's Note: In Java, input is a bit more formal than in other languages. You need to "import" a special worker called the Scanner. 💡


🌟 The Scenario: The Customer Receipt 🧾

Imagine you are at a checkout counter.

  • The Logic: The clerk scans your item (Input). 📦
  • The Result: The total is printed on your receipt (Output). ✅

💻 Implementation: The Scanner Lab

// 🛒 Scenario: Greeting a Customer
// 🚀 Action: Using the Scanner class to read data

import java.util.Scanner; // 📦 1. Import the worker

public class GreetingLab {
public static void main(String[] args) {
// 📦 2. Create the Scanner object
Scanner scanner = new Scanner(System.in);

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

// --- Taking Integer Input (Numbers) 🔢 ---
System.out.print("Enter your age: ");
int age = scanner.nextInt(); // Reads only the next integer
System.out.println("You are " + age + " years old.");

// 📦 3. Close the worker (Best Practice)
scanner.close();
}
}

📖 Key Concepts

  • Scanner: A powerful Java class used to read user input. You must import it first.
  • System.in: This tells the Scanner to "listen" to your keyboard.
  • nextLine(): Used for reading sentences (text).
  • nextInt(): Used for reading whole numbers.
  • close(): Always close your Scanner when finished to save computer resources.

🧠 Step-by-Step Logic

  1. Start 🏁
  2. Bring in the Scanner tool (import).
  3. Set up the Scanner to listen to the keyboard (new Scanner(System.in)).
  4. Prompt the user: "Enter your name: ".
  5. Store the user's typed text into a String variable.
  6. Display the message back.
  7. End 🏁

🎯 Practice Lab 🧪

Task: Calculator

Task: Ask the user for two numbers, add them together, and print the total. Hint: Use scanner.nextInt() twice and a sum variable. 💡

Quick Quiz

Quick Quiz

Which package must you import to use the Scanner class?

  • java.io
  • java.lang
  • java.util
  • java.swing

Explanation: The Scanner class is located in the java.util (Utility) package.


📈 Learning Path

📍 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