-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWordParser.java
51 lines (42 loc) · 1.27 KB
/
WordParser.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
40
41
42
43
44
45
46
47
48
49
50
51
import java.io.File;
import java.util.Scanner;
import java.io.IOException;
import java.util.ArrayList;
public class WordParser implements Parser {
private String filePath;
private File file;
private Scanner scanner;
private ArrayList<String> words = new ArrayList<String>();
WordParser(String filePath_) {
filePath = filePath_;
}
public void createFile() {
file = new File(filePath);
}
public void createScanner() {
try {
scanner = new Scanner(file);
} catch (IOException e) {
// Print the exception along with line number
// using printStackTrace() method
e.printStackTrace();
}
}
public ArrayList<String> parse() {
while (scanner.hasNextLine()) {
String word = scanner.nextLine();
if (word.length() >= 4) {
boolean hasApostrophe = false;
for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == '\'' && word.charAt(i) == ' ') {
hasApostrophe = true;
}
}
if (hasApostrophe == false) {
words.add(word);
}
}
}
return (words);
}
}