-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumofDigits.java
More file actions
43 lines (32 loc) · 753 Bytes
/
Copy pathSumofDigits.java
File metadata and controls
43 lines (32 loc) · 753 Bytes
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
/**
*Given a positive integer, find the sum of its constituent digits.
*/
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class sum_digits {
public static void main(String[] args) {
String file = args[0];
read(file);
}
public static void read(String f) {
try {
BufferedReader in = new BufferedReader(new FileReader(f));
String str;
while ((str = in.readLine()) != null) {
System.out.println(process(str));
}
in.close();
} catch (IOException e) {
}
}
private static String process(String str) {
String s = "";
int a = 0;
for (int i = 0; i < str.length(); i++) {
a += Integer.parseInt(Character.toString(str.charAt(i)));
}
s += a;
return s;
}
}