Java Programming Lesson 8 Part - 2 | Java Methods + Exception Handling + Advanced Input Parsing
Java Programming Lesson 8
✅ Part 1 — Methods (Functions) – Very important
✅ Part 2 — Exception Handling (try–catch)
✅ Part 3 — User Input Advanced (Parsing)
This makes you ready for OOP (Classes & Objects) next 🚀
📘 LESSON-8: Java Methods + Exception Handling + Advanced Input Parsing
✅ Types of Methods in OOP
| Type | Example | Notes |
|---|---|---|
| No return + No parameters | void show() | Simple action |
| Return + Parameters | int sum(int a, int b) | Most used |
| Method Overloading | Same name, different parameters | Very important |
| Static Methods | static | Call without object |
| Non-Static Methods | Needs object | OOP behavior |
✅ Good Example — Strong OOP Concept
✔ Object created → Methods used
✔ This is how OOP works ✅
✅ PART-2: Exception Handling — (try, catch, finally)
Exceptions = Errors at Runtime ⚠
We use try-catch to handle problems without crashing program ✅
✅ Example: Divide by Zero Error Handling
✅ Safe program
✅ No crash
✅ Multiple Exception Handling
✔ Most specific exception first
✔ Exception at bottom
✅ PART-3: Advanced Scanner Input Parsing
Parse = convert String → Number ✅
Useful in real apps 🔥
✔ If user enters text → program still safe ✅
✅ Example: Convert String to Double
✅ Combined Example Program (Best Practice)
✔ Prevents crashes
✔ Proper parsing
✔ Resource closed ✅
🧠 Lesson-8 Summary (Very Important)
| Topic | You Learned ✅ |
|---|---|
| Methods | Reusable code, static & non-static |
| Overloading | Same method name, different parameters |
| try-catch-finally | Safe programs |
| Parsing Input | String → Number conversions |
✅ Practice Tasks (Very Useful)
1️⃣ Create ATM Program
-
deposit(), withdraw(), checkBalance() methods
-
Try-catch for wrong inputs
2️⃣ Take user age as String
→ convert to integer → if invalid show error
3️⃣ Student Marks Calculator
-
3 subjects
-
total marks + average
-
try-catch for wrong input
📌 You are now ready for OOP (Classes & Objects)
🔥 Next lesson will be 100% Practical OOP Implementation
✅ Let’s dive into more exercises from Lesson-8 (Methods, Exception Handling, Advanced Input Parsing) so you can master the concepts before moving to OOP.
📘 Lesson-8: Practice Exercises (Advanced)
Exercise 1 — Calculator with Exception Handling
Requirements:
-
Take two numbers from user input (as String)
-
Take operation choice: +, -, *, /
-
Use try-catch to handle invalid numbers and division by zero
-
Use methods for each operation
Goal:
-
User inputs safely → operations performed → output printed
-
Program never crashes even if input is wrong
Exercise 2 — Age Validation
Requirements:
-
Input age as String
-
Convert to integer using
Integer.parseInt() -
If invalid input → show
"Invalid age" -
If valid → check if age ≥ 18 →
"Adult"else"Minor"
Hint: Use try-catch
Exercise 3 — Student Marks Calculator
Requirements:
-
Input marks for 3 subjects (as String)
-
Convert to integer using
Integer.parseInt() -
Calculate total and average
-
Print result
-
Use methods:
-
int total(int a, int b, int c) -
double average(int total)
-
-
Use try-catch to handle wrong input
Exercise 4 — Password Validator
Requirements:
-
Input password as String
-
Check:
-
Minimum 6 characters
-
Must contain a number
-
-
Use methods for validation
-
Handle exceptions if input is empty or null
Exercise 5 — Advanced Menu Program
Requirements:
-
Display Menu:
-
Take choice input safely using Scanner + parseInt()
-
Use methods for each choice
-
Handle invalid input without crashing program
🔹 Key Learning Outcomes from Exercises
-
Methods → organize your code, reusable
-
Exception handling → make programs safe
-
Parsing input → convert String → numeric safely
-
Combined skills → prepare for OOP concepts
💡 Pro Tip:
Try modifying each program to use static and non-static methods, then create objects to call them. This is a bridge to OOP.

No comments
Thank You For Visit My Website.
I Will Contact As Soon As Possible.