CHAPTER 06
Beginner
User Input and Output
Updated: May 17, 2026
5 min read
# CHAPTER 6
User Input and Output
1. Introduction
So far, our programs have been one-way streets — they only output information. Real applications are interactive — they ask questions and respond based on the user's answers. In this chapter, we'll learn how to read user input from the keyboard using Java'sScanner class.
2. Learning Objectives
-
Import and use the
Scannerclass.
- Read different data types from the keyboard.
- Handle common Scanner pitfalls.
-
Format output with
printf().
- Build an interactive application.
3. The Scanner Class
TheScanner class lives in the java.util package and must be imported.
java
4. Reading Different Data Types
| Method | Reads |
|---|---|
nextLine() | Full line of text (String) |
next() | Single word (stops at space) |
nextInt() | Integer |
nextDouble() | Decimal number |
nextBoolean() | true or false |
nextLong() | Long integer |
java
5. The nextLine() Trap (Critical!)
This is the #1 beginner bug with Scanner:
java
Fix: Add a dummy nextLine() after nextInt() or nextDouble():
java
6. Formatted Output with printf()
| Specifier | Type | Example |
|---|---|---|
%s | String | "Hello" |
%d | Integer | 42 |
%f | Float/Double | 3.14 |
%.2f | 2 decimal places | 3.14 |
%n | New line | Platform-independent |
java
7. String Concatenation with +
java
8. Mini Project: User Information Form
java
9. Common Mistakes
-
Forgetting to import Scanner:
import java.util.Scanner;is required.
-
The nextLine() trap: Always consume the dangling newline after
nextInt()ornextDouble().
-
Not closing the Scanner: Always call
scanner.close()to prevent resource leaks.
-
InputMismatchException: Entering text when
nextInt()expects a number crashes the program.
10. Best Practices
- Always close your Scanner when done.
-
Use
sc.nextLine()after numeric reads to prevent input skipping.
- Validate input when possible (we'll learn exception handling later).
-
Use
printf()for clean, aligned output formatting.
11. Exercises
- 1. Write a program that asks for two numbers and prints their sum, difference, product, and quotient.
- 2. Build a "Mad Libs" style game that asks for a noun, adjective, and verb, then constructs a funny sentence.
- 3. Create a temperature converter that asks for Fahrenheit and converts to Celsius.
12. MCQ Quiz with Answers
Question 1
Which class is used for user input?
Question 2
Which package contains Scanner?
Question 3
Which method reads a full line?
Question 4
What does %d represent in printf?
Question 5
What does %.2f do?
Question 6
What happens if you type "hello" when nextInt() is called?
Question 7
next() stops reading at:
Question 8
How to fix the nextLine() skipping issue?
Question 9
What does scanner.close() do?
Question 10
System.in represents:
13. Interview Questions
-
Q: What is the difference between
next()andnextLine()?
- Q: Explain the newline buffer issue with Scanner and how to fix it.
-
Q: What is
InputMismatchExceptionand when does it occur?
14. Summary
The Scanner class enables interactive Java programs by reading user input from the keyboard. Different methods read different data types. ThenextLine() trap is the most common beginner bug — always consume the trailing newline after numeric reads. Use printf() for formatted output.
15. Next Chapter Recommendation
Now that your programs are interactive, in Chapter 7: Conditional Statements, we'll teach the program to make decisions usingif, else, switch, and build a grade evaluation system.