-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordList.java
More file actions
266 lines (197 loc) · 5.83 KB
/
WordList.java
File metadata and controls
266 lines (197 loc) · 5.83 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package cs2073;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class WordList {
// Stores the list of words
private ArrayList<String> wordsList = new ArrayList<>();
/**
* No-Argument constructor for the WordList class
*/
public WordList() {
}
/**
* Argument constructor for the WordList class which takes the String[] of
* words as the argument
*
* @param array
* the String[] whose elements are to be added to the wordsList
* ArrayList
*/
public WordList(String[] array) {
saveArrayElements(array);
}
/**
* Helper method to save a list of String[] elements into the list of words
* in this class
*
* @param array
* The String[] from which the words are to be read.
*/
private void saveArrayElements(String[] array) {
for (String item : array) {
wordsList.add(item);
}
}
/**
* Method to add all the words from the file into the arraylist of words
*
* @param filename
* The name of the file from which the words are to be read
* @return true if file is opened successfully and false if file isn't
* opened successfully
*/
public boolean readWords(String filename) {
Scanner fileScanner = null;
try {
fileScanner = new Scanner(new File(filename));
} catch (FileNotFoundException e) {
return false;
}
while (fileScanner.hasNext()) {
wordsList.add(fileScanner.next());
}
return true;
}
/**
* Method to return the number of words in the ArrayList of words in the
* WordsList
*
* @return the number of words stored in this WordsList class
*/
public int count() {
return wordsList.size();
}
/**
* Method that returns the number of times the target string is stored in
* the ArrayList of words in this WordsList class
*
* @param target
* The String whose number is to be counted and returned
* @return the number of times target is stored in the WordsList
*/
public int count(String target) {
int targetCount = 0;
for (String item : wordsList) {
if (checkIfEqual(target, item)) {
targetCount++;
}
}
return targetCount;
}
/**
* Helper method to compare two words first and second. While comparing the
* words, the case and punctuation symbols are ignored.
*
* @return true if the words match, false if they don't match
*/
private boolean checkIfEqual(String first, String second) {
if (first.equals(second)) {
return true;
}
// remove punctuation from the two strings
first = removePunctuation(first);
second = removePunctuation(second);
// bring both of the Strings to upper case so as to compare without any
// regard to the case
first = first.toUpperCase();
second = second.toUpperCase();
if (first.equals(second)) {
return true;
}
return false;
}
/**
* Method to remove the punctuation from the String word. It removes the
* punctuation from only the last part of the word
*
* @return the new String after removing punctuation from word
*/
private String removePunctuation(String word) {
word = word.trim();
// the punctuation will be at the last index
char last = word.charAt(word.length() - 1);
if (!Character.isAlphabetic(last)) {
return word.substring(0, word.length() - 1);
}
return word;
}
/**
* Method to replace all the occurrences of the oldString word with the
* newString word. It returns the number of replacements made as a result.
*
* @param oldString
* The word to replace from the list
* @param newString
* The word to replace the oldString word from the list with
* @param ignoreCase
* Whether the case of the oldString should be considered while
* replacing the oldString word
* @return The number of times the oldString word was replaced by the
* newString word
*/
public int replace(String oldString, String newString, boolean ignoreCase) {
int totalReplaced = 0;
for (int i = 0; i < wordsList.size(); i++) {
if (wordsList.get(i).equals(oldString)) {
wordsList.set(i, newString);
totalReplaced++;
continue;
}
if (ignoreCase) {
char char0 = oldString.charAt(0);
String newChar0 = null;
if (Character.isUpperCase(char0)) {
newChar0 = ("" + char0).toLowerCase();
} else {
newChar0 = ("" + char0).toUpperCase();
}
String ignoredCaseOldString = newChar0 + oldString.substring(1);
if (wordsList.get(i).equals(ignoredCaseOldString)) {
wordsList.set(i, newString);
totalReplaced++;
}
}
}
return totalReplaced;
}
/**
* This method displays all the words in the list in a line.
*
* @param wordsPerLine
* the number of words to display in a single line
* @return the total number of lines displayed
*/
public int display(int wordsPerLine) {
int count = 0;
int lines = 1;
boolean first = true;
for (String word : wordsList) {
if (!first && (count % wordsPerLine) == 0) {
System.out.println();
lines++;
}
first = false;
System.out.print(word + " ");
count++;
}
return lines;
}
/**
* Method to return a String containing the contents of the list, surrounded
* by [ ] brackets. Individual words should be separated by single spaces.
*
* @return The string with the content of the list
*/
public String toString() {
String returnString = "[";
for (int i = 0; i < wordsList.size(); i++) {
returnString += wordsList.get(i);
if (i != (wordsList.size() - 1)) {
returnString += " ";
}
}
return returnString + "]";
}
}