-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathSumHex.java
More file actions
29 lines (27 loc) · 911 Bytes
/
SumHex.java
File metadata and controls
29 lines (27 loc) · 911 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
public class SumHex{
static int strToInt(String str) {
if (str.startsWith("0x") || str.startsWith("0X")) {
return Integer.parseUnsignedInt(str.substring(2), 16);
} else {
return Integer.parseInt(str);
}
}
public static void main(String[] args) {
int res = 0;
for (int i = 0; i < args.length; i++) {
int numStart = 0;
for (int j = 0; j < args[i].length(); j++) {
if (Character.isWhitespace(args[i].charAt(j))) {
if (j - numStart > 0) {
res += strToInt(args[i].substring(numStart, j));
}
numStart = j + 1;
}
}
if (numStart < args[i].length()) {
res += strToInt(args[i].substring(numStart));
}
}
System.out.println(res);
}
}