-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task1.java
39 lines (36 loc) · 1.09 KB
/
Task1.java
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 org.example;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.util.ArrayList;
import java.util.List;
public class Task1 {
private List<String> validNumbers = new ArrayList<>();
public void execute(String path){
readFile(path);
printNumbers();
}
private void printNumbers(){
for(String number:validNumbers){
System.out.println(number);
}
}
private void readFile(String path){
try(BufferedReader reader = new BufferedReader(new FileReader(path))) {
String line;
while((line = reader.readLine())!=null){
if(isValidNumber(line)){
validNumbers.add(line);
}
}
}
catch (IOException ex){
System.out.println(ex.getMessage());
}
}
private boolean isValidNumber(String line) {
String pattern1 = "\\(\\d{3}\\)-? \\d{3}-?\\d{4}";
String pattern2 = "(?:\\d{3}-){2}\\d{4}";
return line.matches(pattern1) || line.matches(pattern2);
}
}