Repository navigation
Expand file tree
/
Copy pathSpellCheck.java
More file actions
74 lines (73 loc) · 2.43 KB
/
Copy pathSpellCheck.java
File metadata and controls
74 lines (73 loc) · 2.43 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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.io.*;
public class SpellCheck {
static String s;
static int ch;
static int lineNum = 1;
static void readWord(InputStream fIn) {
try {
while (true)
if (ch > -1 && !Character.isLetter((char)ch)) { // skip
ch = fIn.read(); // non-letters;
if (ch == '\n')
lineNum++;
}
else break;
if (ch == -1)
return;
s = "";
while (ch > -1 && Character.isLetter((char)ch)) {
s += Character.toUpperCase((char)ch);
ch = fIn.read();
}
} catch (IOException io) {
System.out.println("Problem with input.");
}
}
static public void main(String args[]) {
String fileName = "";
InputStream fIn, dictionary;
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader buffer = new BufferedReader(isr);
Trie trie = null;
try {
dictionary = new FileInputStream("dictionary");
readWord(dictionary);
trie = new Trie(s.toUpperCase()); // initialize root;
while (ch > -1) {
readWord(dictionary);
if (ch == -1)
break;
trie.insert(s);
}
dictionary.close();
} catch(IOException io) {
System.err.println("Cannot open dictionary");
}
System.out.println("\nTrie: ");
trie.printTrie();
ch = ' ';
lineNum = 1;
try {
if (args.length == 0) {
System.out.print("Enter a file name: ");
fileName = buffer.readLine();
fIn = new FileInputStream(fileName);
}
else {
fIn = new FileInputStream(args[0]);
fileName = args[0];
}
System.out.println("Misspelled words:");
while (true) {
readWord(fIn);
if (ch == -1)
break;
if (!trie.found(s))
System.out.println(s + " on line " + lineNum);
}
fIn.close();
} catch(IOException io) {
System.err.println("Cannot open " + fileName);
}
}
}