-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSumDigits.java
More file actions
50 lines (42 loc) · 1.34 KB
/
SumDigits.java
File metadata and controls
50 lines (42 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/**
* Write a program to sum up the individual digits of a positive integer,
* given in the command line. The output shall look like:
*
* > java SumDigits 12345
* The sum of digits = 1 + 2 + 3 + 4 + 5 = 15
*/
package javaexercises.cmdline;
public class SumDigits {
public static void main(String[] args) {
SumDigits aSumDigits = new SumDigits();
if (args.length == 0) {
System.out.println("There are no arguments.");
return;
}
try {
int number = Integer.parseInt(args[0]);
if (number < 0) {
System.out.printf("Argument \"%1$s\" is not a positive integer \n", args[0]);
return;
}
aSumDigits.run(number);
}
catch (NumberFormatException e) {
System.out.printf("Argument \"%1$s\" is not an integer \n", args[0]);
}
}
/**
*
*/
private void run(int number)
{
String tmp = String.valueOf(number);
int sum = 0;
System.out.print("The sum of digits = ");
for (int i = 0; i < tmp.length(); i++) {
sum += Character.digit(tmp.charAt(i), 10);
System.out.print( (i == 0 ? "" : " + ") + tmp.charAt(i));
}
System.out.print(" = " + sum + "\n");
}
}