-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDictionary.java
More file actions
33 lines (28 loc) · 1.08 KB
/
Dictionary.java
File metadata and controls
33 lines (28 loc) · 1.08 KB
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
import java.io.*;
import java.util.*;
public class Dictionary implements Serializable {
private static final long serialVersionUID = 1L;
private Set<String> words;
public Dictionary(String filename) {
words = new HashSet<>();
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = br.readLine()) != null) {
line = line.trim(); // remove leading/trailing whitespace and newlines
if (!line.isEmpty()) {
words.add(line.toUpperCase());
}
}
} catch (IOException e) {
System.out.println("Error loading dictionary: " + e.getMessage());
}
}
public Set<String> getAllWords() {
return new HashSet<>(words); // returns a copy of all words
}
public boolean isValidWord(String word) {
if (word == null) return false;
word = word.trim().toUpperCase(); // trim spaces and convert to uppercase
return words.contains(word);
}
}