Commit 491b333
committed
feat: Add StringExample class with greeting methods and user input
WHAT the code does:
- Defines a `StringExample` class that demonstrates **string handling** with methods.
- Uses `Scanner` to take user input (a name).
- Calls `myGreet(String)` to return a personalized greeting.
- Optionally has `greet()` that returns a static message ("how are you").
- Prints the personalized greeting to the console.
WHY this matters:
- Demonstrates how to create and return strings from methods.
- Shows **string concatenation** for dynamic message creation.
- Reinforces **method calling** and passing arguments in Java.
- Introduces **user input handling** with `Scanner`.
HOW it works:
1. Program prompts → `"Enter your name:"`.
2. Reads input using `in.next()` and stores it in `naam`.
3. Calls `myGreet(naam)` which returns `"Hello <name>"`.
4. Prints personalized greeting (e.g., `"Hello Rahul"`).
5. `greet()` exists as a simple utility returning `"how are you"` (not used in main, but available).
Tips & gotchas:
- `Scanner` should be closed after use (`in.close()`) to prevent resource leaks.
- `in.next()` reads only a single word; use `in.nextLine()` if full names with spaces are needed.
- String concatenation creates new objects internally → consider `StringBuilder` for heavy usage.
- Method names could follow clearer naming conventions (e.g., `getGreeting()`).
Use-cases:
- Basic user interaction programs.
- Teaching examples for methods and return values.
- Building blocks for chatbots or text-based interfaces.
- Can be extended to add multiple greeting styles (formal, casual, etc.).
Short key: class-string example-greeting-user input.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent c26a7fe commit 491b333
1 file changed
+5
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | 3 | | |
4 | | - | |
5 | 4 | | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | 5 | | |
10 | 6 | | |
| 7 | + | |
11 | 8 | | |
| 9 | + | |
12 | 10 | | |
13 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | | - | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
0 commit comments