Skip to content

Commit 0b30607

Browse files
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

File tree

1 file changed

+1
-2
lines changed

1 file changed

+1
-2
lines changed

Section10Methods/src/AddingNumber.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ public class AddingNumber {
22
public static void main(String[] args) {
33
double s = 0;
44

5-
for(String x: args)
6-
{
5+
for(String x: args) {
76
//if (x.matches("[0-9\\.]+"))
87
s=s+Double.parseDouble(x);
98
}

0 commit comments

Comments
 (0)