简体   繁体   English

将 .txt 文件中的所有单词添加到自定义对象的 ArrayList

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

I have a .txt file in which every line has a single word.我有一个 .txt 文件,其中每一行都有一个单词。 I've created a class called Word with a string variable and ArrayList variable.我创建了一个名为Word的类,其中包含一个字符串变量和 ArrayList 变量。 My objective is to loop through the entire .txt file and store every word in there as an Word object.我的目标是遍历整个 .txt 文件并将其中的每个单词作为Word对象存储。 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]例如,如果单词是“philly”,那么将使用 String philly创建类 Word 的对象,并创建一个 Character ArrayList 为[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.下面的代码将用于将 .txt 文件中的每个单词存储在 String ArrayList 中,但我对这个项目的目标已经改变,我现在需要一种方法来将它存储为具有两个变量的对象,而不仅仅是字符串。 The line that I specifically need to replace is objectArray.fillWord(strLine);我特别需要替换的行是objectArray.fillWord(strLine); . . It is currently giving me the error message The method fillWord (String) is undefined for the type ArrayList <Word> .它目前给我错误消息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?如何更改此设置以将特定 Word 对象添加到 Word 对象的 objectArray? 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?如何在此循环中为 txt 文件中的每个单词创建一个实例,使每个单词与 txt 文件中的名称相同? 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;
    }

}
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这会将 fillWord 变成一个构造函数,允许您将有问题的行更改为

objectArray.add(new Word(strLine));

Unrelated side note: I noticed you named the arraylist unique letters.无关的旁注:我注意到您将 arraylist 命名为唯一字母。 Would you need to filter repeated letters?您需要过滤重复的字母吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM