I've made a program that works, but i want to make it reusable. This code reads in a dictionary object created in another class (that i made), as well as read in a text file to put in said dictionary object. The code then loads in another text file, compares it with the dictionary, updates its frequency values and outputs to a vector. Everything works, but as i said i'm looking to make it reusable. What i mean by this is once the values have been stored in the vector, i'd like the dictionaries frequency values to reset back to 0, ready for the next text document to be compared with the dictionary, then output that to a different vector - this process will be repeated 10-12 times.
String Counter
public class StringCounter {
private LinkedList<Integer> list = new LinkedList<>();
public StringCounter(LinkedList<Integer> list) {
this.list = list;
}
public static void main(String[] args) {
HashMap<String, Integer> dictionary = new HashMap<String, Integer>();
List<String> textFileList = Arrays.asList("Test.txt", "Test2.txt");
try {
Dictionary reader = new Dictionary(dictionary);
for (String text : textFileList) {
reader.fileScanner(text);
}
Scanner textFile = new Scanner(new File("Test4.txt"));
ArrayList<String> file = new ArrayList<String>();
while(textFile.hasNext()) {
file.add(textFile.next().trim().toLowerCase());
}
for(String word : file) {
Integer dict = dictionary.get(word);
if (!dictionary.containsKey(word)) {
dictionary.put(word, 1);
} else {
dictionary.put(word, dict + 1);
}
}
textFile.close();
} catch(FileNotFoundException e){
e.printStackTrace();
}
Vector<Integer> vec1 = new Vector<>(dictionary.values());
for (Integer count : vec1) {
System.out.println(count);
}
}
}
Dictionary
public class Dictionary {
// Declare set in a higher scope (making it a property within the object)
private HashMap<String, Integer> dictionary = new HashMap<String, Integer>();
// Assigns the value of the parameter to the field of the same name
public Dictionary(HashMap<String, Integer> dictionary) {
this.dictionary = dictionary;
}
// Gets input text file, removes white spaces and adds to dictionary object
public void fileScanner(String textFileName) {
try {
Scanner textFile = new Scanner(new File(textFileName));
while (textFile.hasNext()) {
dictionary.put(textFile.next().trim(), 0);
}
textFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void printDict(HashMap<String, Integer> dictionary) {
System.out.println(dictionary.keySet());
}
}