java - 识别文件中的每个单词

标签 java arraylist import bufferedreader filereader

导入大量单词列表,我需要创建能够识别文件中每个单词的代码。我正在使用分隔符来识别每个单词的分隔,但收到一条抑制错误,指出未使用行号和分隔符的值。我需要做什么才能让程序读取该文件并分隔该文件中的每个单词?

public class ASCIIPrime {
    public final static String LOC = "C:\\english1.txt";

    @SuppressWarnings("null")
    public static void main(String[] args) throws IOException {

        //import list of words 
        @SuppressWarnings("resource")
        BufferedReader File = new BufferedReader(new FileReader(LOC)); 

        //Create a temporary ArrayList to store data
        ArrayList<String> temp = new ArrayList<String>();

        //Find number of lines in txt file
        String line;
        while ((line = File.readLine()) != null)
        {
            temp.add(line);
        }

        //Identify each word in file
        int lineNumber = 0; 
        lineNumber++;
        String delimiter = "\t";

        //assess each character in the word to determine the ascii value 
        int total = 0; 
        for (int i=0; i < ((String) line).length(); i++)
        {
            char c = ((String) line).charAt(i);
            total += c;
        }

        System.out.println ("The total value of " + line + " is " + total); 
    }
}

最佳答案

这听起来像家庭作业,但没关系。

Importing a large list of words and I need to create code that will recognize each word in the file. What do I need to do to get the program to read this file and to separate each word within that file?

你需要...

  • 阅读文件
  • 将单词与您读过的内容分开
  • ...我不知道之后你想用它们做什么。我会将它们转储到一个大列表中。

我的主要方法的内容是...

BufferedReader File = new BufferedReader(new FileReader(LOC));//LOC is defined as class variable

//Create an ArrayList to store the words
List<String> words = new ArrayList<String>();

String line;
String delimiter = "\t";
while ((line = File.readLine()) != null)//read the file
{
    String[] wordsInLine = line.split(delimiter);//separate the words
    //delimiter could be a regex here, gotta watch out for that
    for(int i=0, isize = wordsInLine.length(); i < isize; i++){
        words.add(wordsInLine[i]);//put them in a list
    }
}

关于java - 识别文件中的每个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24759252/

相关文章:

java - 如何从自定义 ArrayList 中获取特定条目?

java - Android 问题记录 ArrayList

java - 检测并缩短字符串中的所有网址

java - 根据标签值的变化将 xml 拆分为更小的 XML

java - 我无法从列表中检索数组值

scala - 为什么当 scala.language.implicitConversions 不是最后一次导入时发出警告?

python - 理解 python 导入

java - ubuntu 上的 izpack 安装程序 'is not marked as executable'

java - 如何在spring boot中启用浏览器缓存

java - ClassA 内的 ClassA 导入语句?为什么设计师允许这样做?