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
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.
Frequently Asked Questions-(FAQ)
What are methods in Java?
Methods in Java are blocks of reusable code that perform a specific task. They help reduce repetition, improve readability, and make programs modular and easy to maintain.
Why are methods important in Java programming?
Methods allow developers to break complex programs into smaller, manageable pieces. They support code reusability, simplify debugging, and make object-oriented programming more efficient.
What is method overloading in Java?
Method overloading occurs when multiple methods share the same name but differ in parameters. It increases flexibility and allows functions to handle different types of inputs.
What is exception handling in Java?
Exception handling is a mechanism that manages runtime errors using try, catch, throw, throws, and finally blocks. It prevents program crashes and ensures smooth execution.
Why do we use try-catch blocks in Java?
A try-catch block is used to detect and handle exceptions. The try block contains risky code, while the catch block handles specific errors to avoid program failure.
What is the difference between checked and unchecked exceptions?
Checked exceptions must be handled during compilation (e.g., IOException), while unchecked exceptions occur at runtime and do not require explicit handling (e.g., ArithmeticException).
What is input parsing in Java?
Input parsing converts user input (String) to data types like int, double, or boolean using methods such as Integer.parseInt(), Double.parseDouble(), and Boolean.parseBoolean().
What exceptions can occur during input parsing in Java?
Input parsing ensures the data collected from users is correctly converted to the required variable types, making programs more reliable and preventing type errors.
Why is input parsing important for Java programs?
The most common exception during input parsing is NumberFormatException, which occurs when a non-numeric value is parsed into a number type.
How can we handle invalid user input in Java?
You can handle invalid input using try-catch blocks, loops to re-ask for input, and validation methods to ensure the data type is correct before processing.
What is the Scanner class used for?
The Scanner class is used to take input from the user, files, or other input streams. It can read different data types like int, double, and String.
How can we avoid Scanner input issues like skipping lines?
Use nextLine() after nextInt() or nextDouble() to clear the buffer. Scanner often leaves newline characters that cause input skipping.

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