§ — — Object-Oriented Programming with Java
This lesson explains how Java programs receive input and how they control the order of execution using decision statements and loops.
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.
ProReviewer — locked
Drills, code labs, and full solutions.
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 |
ProReviewer — locked
Drills, code labs, and full solutions.
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
ProReviewer — locked
Drills, code labs, and full solutions.
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.
ProReviewer — locked
Drills, code labs, and full solutions.
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 |
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';
}
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.
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 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.
ProReviewer — locked
Drills, code labs, and full solutions.
ProReviewer — locked
Drills, code labs, and full solutions.
Modify the grade value and observe which branch runs. Then try changing the loop limit.
Done with this module? Track it — your progress shows on the subject list.
Up next
Arrays and Strings→←Previous: Introduction to Object-Oriented Programming and Java Basics