Adding all the words from a .txt file to an ArrayList of Custom objects

FinDev :

I have a .txt file in which every line has a single word. I've created a class called Word with a string variable and ArrayList variable. My objective is to loop through the entire .txt file and store every word in there as an Word object. For example, if the word is "philly", then an object of the class Word would be created with the String philly and a Character ArrayList would be created as [p,h,i,l,l,y]

The code below will work to store every word in the .txt file in a String ArrayList but my objective with this project has since changed and I now need a way to store it as objects with both variables, not just the string. The line that I specifically need to replace is objectArray.fillWord(strLine);. It is currently giving me the error message The method fillWord (String) is undefined for the type ArrayList <Word>. How do I alter this to add a specific Word object to the objectArray of Word objects? How can I create an instance for every single word in the txt file within this loop that gives each Word the same name as it has in the txt file? Any help is truly appreciated!

import java.awt.List;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Set;

public class WordRecommender {

public static void main(String[] args) {

   StringBuilder sb = new StringBuilder();
   String strLine = "";
   ArrayList<Word> objectArray = new ArrayList<Word>();
   try {
        BufferedReader br = new BufferedReader(new FileReader("engDictionary.txt"));
         while (strLine != null)
          {
           strLine = br.readLine();
           sb.append(strLine);
           sb.append(System.lineSeparator());
           strLine = br.readLine();
           if (strLine==null)
              break;
           objectArray.fillWord(strLine);
       }
        br.close();
   } catch (FileNotFoundException e) {
       System.err.println("File not found");
   } catch (IOException e) {
       System.err.println("Unable to read the file.");
   }
}
import java.util.ArrayList;

public class Word {

    String wordName;
    ArrayList<Character> uniqueLetters;

    public Word(String wordName, ArrayList<Character> uniqueLetters) {
        this.wordName = wordName;
        this.uniqueLetters = uniqueLetters;
    }

    public Word fillWord(String string) {

        ArrayList<Character> tempArray = new ArrayList<Character>();

        for (int i = 0; i < string.length(); i++) { 
                        tempArray.add(string.charAt(i));
                    }

        Word w = new Word(string, tempArray);

        return w;
    }

}
Arch2K :
import java.util.ArrayList;

public class Word {

    String wordName;
    ArrayList<Character> uniqueLetters;

    public Word(String wordName, ArrayList<Character> uniqueLetters) {
        this.wordName = wordName;
        this.uniqueLetters = uniqueLetters;
    }

    public Word(String string) {
        ArrayList<Character> tempArray = new ArrayList<Character>();

        for (int i = 0; i < string.length(); i++) { 
                        tempArray.add(string.charAt(i));
                    }

        this.wordName = string;
        this.uniqueLetters = tempArray;
    }

}

This would turn fillWord into a constructor allowing you to change the problematic line to

objectArray.add(new Word(strLine));

Unrelated side note: I noticed you named the arraylist unique letters. Would you need to filter repeated letters?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=17845&siteId=1