forked from janbodnar/Java-Advanced
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaRegexSplitText.java
More file actions
39 lines (24 loc) · 936 Bytes
/
JavaRegexSplitText.java
File metadata and controls
39 lines (24 loc) · 936 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
package com.zetcode;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.regex.Pattern;
public class JavaRegexSplitText {
static int sum = 0;
public static void main(String[] args) throws IOException {
Path myPath = Paths.get("src/main/resources/data.csv");
List<String> lines = Files.readAllLines(myPath);
String regex = ",";
Pattern p = Pattern.compile(regex);
lines.forEach((line) -> {
String[] parts = p.split(line);
for (String part : parts) {
String val = part.trim();
sum += Integer.valueOf(val);
}
});
System.out.printf("Sum of values: %d", sum);
}
}