Commit 0b30607
committed
feat: Add AddingNumber class to sum command-line arguments
WHAT the code does:
- Defines an `AddingNumber` class with a `main()` method.
- Initializes sum variable `s = 0`.
- Iterates over command-line arguments (`args`).
- Parses each argument as a `double` and adds it to the sum.
- Prints the total sum.
WHY this matters:
- Demonstrates handling **command-line arguments** in Java.
- Shows how to **parse strings into numeric values** (`Double.parseDouble`).
- Reinforces iteration using enhanced `for` loops.
HOW it works:
1. Run program with numbers as arguments:
java AddingNumber 10 20 30.5
2. Each string argument is parsed to a double.
3. Sum accumulates (`10 + 20 + 30.5 = 60.5`).
4. Output: `"Sum is 60.5"`.
Tips & gotchas:
- If a non-numeric argument is passed, `NumberFormatException` will be thrown.
- Commented regex (`x.matches("[0-9\\.]+")`) could be re-enabled to filter invalid inputs.
- Floating-point precision issues may occur for large sums — use `BigDecimal` if needed.
- For integer-only inputs, `Integer.parseInt` would be more appropriate.
Use-cases:
- Educational demo for **command-line argument parsing**.
- Simple utility to sum numbers provided at runtime.
- Can be extended to handle subtraction, multiplication, or average.
- Useful practice for error handling and input validation.
Short key: class-adding number-cli-sum.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent e2242bb commit 0b30607
1 file changed
+1
-2
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | | - | |
| 5 | + | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
| |||
0 commit comments