I need to use ArrayLists to count the words in a text file and display their frequency. I would like to start by creating the ArrayList of "Word" objects. From that point I shouldn't have an issue. The problem I am encountering is when adding an object to the list. I receive an error stating "The method add(Word) in the type ArrayList is not applicable for the arguments (String)"
public ArrayList<Word> wordList = new ArrayList<Word>();
String fileName, word;
int counter;
Scanner reader = null;
Scanner scanner = new Scanner(System.in);
public void analyzeText() {
System.out.print("Please indicate the file that you would like to analyze (with the path included): ");
fileName = scanner.nextLine();
try {
reader = new Scanner(new FileInputStream(fileName));
}
catch(FileNotFoundException e) {
System.out.println("The file could not be found. The program will now exit.");
System.exit(0);
}
while (reader.hasNext()) {
word = reader.next().toLowerCase();
wordList.add(word);
counter++;
}
}
public class Word {
String value;
int frequency;
public Word(String v) {
value = v;
frequency = 1;
}
}
Wordobject as it is expecting.wordList.add(new Word(word));Wordclass name or thewordfield name. The field namerawValueorstringValuewould make the code easier to read and the error easier to spot