Lesson Overview
This lesson explains how Java programs receive input and how they control the order of execution using decision statements and loops.
Keyboard Input with BufferedReader
BufferedReader can read text from the keyboard. Since it reads input as text, numeric values must be converted before computation.
Common conversions:
| Needed value | Example conversion |
|---|---|
int | Integer.parseInt(reader.readLine()) |
double | Double.parseDouble(reader.readLine()) |
String | reader.readLine() |
When using BufferedReader, import java.io.* and handle IOException.
Activity: BufferedReader Input
Keyboard Input with Scanner
Scanner is commonly used for beginner Java input because it provides methods for different data types.
| Method | Reads |
|---|---|
nextInt() | An integer |
nextDouble() | A decimal number |
next() | One word |
nextLine() | An entire line |
Activity: Scanner Input
Command-Line Arguments and Parsing
Values passed after the program name can be accessed through args. Since each command-line argument is a String, numeric input must be parsed.
Example run:
java CommandLineDemo Ana 19 50.5
Activity: Command-Line Argument Parsing
Dialog Box Input and Output
JOptionPane from javax.swing can show simple input and message dialog boxes.
Useful methods:
| Method | Purpose |
|---|---|
showInputDialog() | Gets text input from the user |
showMessageDialog() | Displays a message dialog |
Dialog message types include ERROR_MESSAGE, INFORMATION_MESSAGE, WARNING_MESSAGE, QUESTION_MESSAGE, and PLAIN_MESSAGE.
Activity: Add Two Numbers with Dialog Boxes
Control Flow Statements
Control flow statements decide which statements run, how often they run, and when execution should stop or jump.
| Category | Common statements |
|---|---|
| Decision | if, else, switch |
| Looping | for, while, do-while |
| Exception handling | try, catch, finally, throw |
| Flow change | break, continue, return |
If, Else, and Else If
An if statement runs code only when a condition is true. An else block runs when the condition is false. An else if chain checks multiple conditions in order.
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else {
grade = 'D';
}
Switch Statement
A switch statement chooses a block of code based on a single expression. It is useful when comparing one value against several exact choices.
switch (month) {
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
default:
System.out.println("Invalid month");
}
Use break to stop execution from falling into the next case.
Loop Statements
Loops repeat code while a condition is true.
| Loop | Best use |
|---|---|
for | When the number of repetitions is known |
while | When repetition depends on a condition checked before each run |
do-while | When the loop body should run at least once |
for (int i = 1; i <= 5; i++) {
System.out.println(i);
}
int i = 1;
while (i <= 5) {
System.out.println(i);
i++;
}
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 5);
Break, Continue, and Nested Loops
break immediately exits a loop. continue skips the remaining statements in the current iteration and proceeds to the next iteration.
Nested loops are loops placed inside other loops. They are useful for tables, patterns, and grid-like output.
Activity: Nested Loop Pattern
Laboratory Exercises
Try It: Control Statements
Modify the grade value and observe which branch runs. Then try changing the loop limit.