0

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;
    }

}
6
  • 8
    Don't add a String to the ArrayList! Add a Word object as it is expecting. Commented Nov 22, 2018 at 2:43
  • 3
    In fact the error message is telling you exactly what is wrong in a non-confusing way. Be sure to read those messages critically as they hold the key to your problems and their solutions. Commented Nov 22, 2018 at 2:43
  • 6
    change to wordList.add(new Word(word)); Commented Nov 22, 2018 at 2:44
  • 1
    You should probably rename the Word class name or the word field name. The field name rawValue or stringValue would make the code easier to read and the error easier to spot Commented Nov 22, 2018 at 2:46
  • 1
    Just becaus e a String object is not a Word object Commented Nov 22, 2018 at 2:47

3 Answers 3

2

You need to add a Word Object not a String:

word = reader.next().toLowerCase();
Word myNewWord = new Word(word); /*Generates a Word Object using your constructor*/
wordList.add(myNewWord);
counter++

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

1

wordList is an array of "Word" objects. But in line 17

wordList.add(word);

you're adding another type of content into the array (a string).

Note there's an object-type, named "Word" (uppercase), and another variable named "word" (lowercase) of type string.

You're adding a string "word" to the array list, but in this case you can add only objects "Word" to the ArrayList of name wordList.

Comments

1

You need to add Word object to your list. But you are assigning a string which is readed from scanner. You need to create a Word object.

I think, your solution for counting word is wrong. You are using wrong data structure. Hashmap fits better for this case. You can assign words as a key and count of words as a value.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.