forked from bytao7mao/java_simple_algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumUpNumbers.java
More file actions
28 lines (25 loc) · 862 Bytes
/
sumUpNumbers.java
File metadata and controls
28 lines (25 loc) · 862 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
private static int sumUpNumbers(String inputString) {
int count = 0;
Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher(inputString);
while (m.find()) {
count+=Integer.parseInt(m.group());
System.out.println(m.group());
}
//variant #2
// String[] splittingTheNumbers=inputString.split("\\D+");
// int count=0;
// for (String num:splittingTheNumbers){
// if (num.length()>0) count+=Integer.parseInt(num);
// }
// return count;
//variant #3
// int count=0;
// Matcher m = Pattern.compile("\\d+").matcher(inputString);
// while (m.find()) {
// count+=Integer.parseInt(m.group());
// }
// return count;
System.out.println(count);
return count;
}