-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtractIntegers.java
More file actions
41 lines (31 loc) · 816 Bytes
/
ExtractIntegers.java
File metadata and controls
41 lines (31 loc) · 816 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
import java.lang.Character;
import java.util.List;
import java.util.ArrayList;
public class ExtractIntegers {
public static void main(String[] args) {
String s = "cv dd k k 2321 2 11 k4k2 66 4d";
//2. Build and example
//a. 34 1sda 23 aa (34, 23)
//b. df 1 f 34 1 (34)
//c. cv dd k k 2321 2 11 k4k2 66 4d (4,2321,2,11,4,2,66,4)
//0123456789
//0123456789
//0123456789
List<Integer> result = new ArrayList<>();
StringBuilder temp = new StringBuilder();
for(int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
if(Character.isDigit(ch)) {
temp.append(ch);
} else {
if(temp.length() > 0) {
result.add(Integer.valueOf(temp.toString()));
temp.delete(0, temp.length());
}
}
}
result
.stream()
.forEach(System.out::println);
}
}