Chapter 5: Input and Output

1. Why Input & Output Matters

Until now, all our values were hardcoded (like int age = 25;). That’s fine for learning, but in real programs you want the user to enter their own data — name, age, marks, etc. And you want to show output in a pretty, formatted way — not just plain println.

Java gives us two main ways to read input from the user (keyboard):

  • Scanner class → easiest and most popular for beginners
  • BufferedReader → faster and more powerful (used in advanced/enterprise code)

We’ll learn both, but start with Scanner — you’ll use it 90% of the time.

2. Reading Input Using Scanner Class

Step 1: Import Scanner

Java

Step 2: Create a Scanner object

Java
  • System.in → means “read from keyboard”

Step 3: Read different types of input

Method What it reads Example Code
nextInt() Integer (whole number) int age = sc.nextInt();
nextDouble() Decimal number double salary = sc.nextDouble();
nextFloat() Float (less precise decimal) float height = sc.nextFloat();
next() One word (until space) String firstName = sc.next();
nextLine() Full line (including spaces) String fullName = sc.nextLine();
nextBoolean() true or false boolean isStudent = sc.nextBoolean();

Complete Example Program — Let’s make a fun “Personal Info Collector”

Java

Important Tip — The nextLine() Trap (Common Mistake!)

After nextInt(), nextDouble(), etc., there is a leftover newline character (\n) in the input buffer. When you call nextLine() after that, it reads an empty line!

Solution: Add sc.nextLine(); right after reading numbers to consume the leftover \n.

Example of the problem:

Java

Fix:

Java

3. Reading Input Using BufferedReader (Advanced Way)

This is older but faster and used in many real-world apps.

Java

Pros of BufferedReader:

  • Faster for large input
  • Reads everything as String → you convert manually

Cons:

  • More code
  • Need to handle IOException

For now → use Scanner — it’s perfect for beginners and most college/projects.

4. Beautiful Output Formatting

Java gives you two powerful ways to print nicely formatted output:

A. System.out.println() and print() (Basic)

You already know these — but you can make them pretty with concatenation and escape sequences:

Java

B. printf() — The Professional Way (like C language)

printf = print formatted

Syntax:

Java

Common Format Specifiers:

Specifier Meaning Example Output
%d Integer (whole number) %d 25
%f Floating point (decimal) %.2f (2 decimal places) 85.50
%s String %s Webliance
%c Character %c A
%b Boolean %b true
%% Print % sign %% %
\n New line

Real Example — Beautiful Bill Receipt

Java

Output looks like this:

text

Tips for printf:

  • %-15s → left-align, width 15 characters
  • %,.2f → add commas for thousands + 2 decimal places

Homework for You (Fun Practice!)

  1. Create a program called StudentInfo.java:
    • Ask user: name (nextLine), roll number (nextInt), percentage (nextDouble), section (next — single char like ‘A’)
    • Print everything in a beautiful formatted table using printf
  2. Make a Simple Calculator:
    • Ask for two numbers
    • Ask for operation (+, -, *, /)
    • Show result with 2 decimal places
  3. Try the nextLine() trap intentionally — see what happens, then fix it!

You’re doing amazing! Now your programs can talk to users and look professional.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *