-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDictionary.java
More file actions
185 lines (144 loc) · 4.51 KB
/
Copy pathDictionary.java
File metadata and controls
185 lines (144 loc) · 4.51 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Dictionary {
static Map<String, Word> m = new HashMap<String, Word>();
public static void main(String[] args) {
BufferedReader dictionaryReader = null;
File fileName = new File("Dictionary.txt");
int currIndex = -1;
Word newWord;
DictionaryGUI display = new DictionaryGUI();
String first4 = "";
String word = "";
String currentLine = "";
Word.Gtype currType = null;
try {
dictionaryReader = new BufferedReader(new FileReader(fileName));
// to remove the first line saying "Dictionary:"
dictionaryReader.readLine();
System.out.println("\nReading data from " + fileName + ": ");
while ((currentLine = dictionaryReader.readLine()) != null) {
if (currentLine.length() >= 4) {
if (currentLine.contains(";")) {
first4 = currentLine.substring(0, 4);
currentLine = currentLine.substring(4);
switch (first4) {
case "verb":
currType = Word.Gtype.verb;
break;
case "noun":
currType = Word.Gtype.noun;
break;
case "adje":
currType = Word.Gtype.adjective;
break;
}
currIndex = currentLine.indexOf(";");
word = currentLine.substring(0, currIndex);
currentLine = currentLine.substring(currIndex + 1);
word = word.trim();
word = word.substring(0, 1).toUpperCase() + word.substring(1);
currentLine.trim();
newWord = new Word(word, currentLine, currType);
// System.out.println(newWord);
m.put(word, newWord);
word = "";
currentLine = "";
currType = null;
}
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (dictionaryReader != null)
System.out.println("Dictionary Has been initialized with all the words");
dictionaryReader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
// System.out.println(m.size() + " distinct words:");
// System.out.println(m);
}
public static Word getDicWord(String word) {
Word thisWord = m.get(word);
return thisWord;
}
public static boolean isWord(String word) {
return m.containsKey(word);
}
}
class DictionaryGUI implements ActionListener {
JFrame frame = new JFrame("The Best Java Dictionary");
JLabel label = new JLabel("Enter a Word to get its definition: ");
JLabel definition = new JLabel("");
JButton defineButton = new JButton("Define");
JTextField textBox = new JTextField(20);
Dictionary myDic = new Dictionary();
// Creating the panel at bottom and adding components
JPanel panel = new JPanel();
JPanel panel2 = new JPanel();
public DictionaryGUI() {
defineButton.addActionListener(this);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setBorder(BorderFactory.createEmptyBorder(100, 100, 100, 100));
panel.setLayout(new GridLayout(3, 2));
panel2.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
// panel2.setLayout(new GridLayout(0, 1));
panel.add(label);
panel.add(textBox);
panel.add(defineButton);
panel2.add(definition);
frame.add(panel, BorderLayout.NORTH);
frame.add(panel2, BorderLayout.CENTER);
frame.pack();
frame.setSize(1000, 400);
frame.setVisible(true);
}
public String getWord() {
String word = "";
if (textBox.getText() != null) {
word = textBox.getText();
}
return word;
}
/**
* @Override
*
* This method will execute when the "Define" button is pressed
*
*
*/
public void actionPerformed(ActionEvent e) {
if (!textBox.getText().isEmpty()) {
String word = textBox.getText();
word = word.trim();
word = word.substring(0, 1).toUpperCase() + word.substring(1);
if (Dictionary.isWord(word))
show(Dictionary.getDicWord(word));
else
definition.setText("Word Not in Dictionary.\n Try Again");
} else {
definition.setText("Please, enter a Word before pressing Button!");
}
}
public void show(Word word) {
definition.setText(word.toString());
}
}