将文本文件放入ArrayList,但如果存在单词,则跳过它

时间:2016-10-06 21:14:18

标签: java list file arraylist

我在这里有点挣扎,我试图将每个单词从文本文件添加到ArrayList,每次读者再次遇到同一个单词时,它都会跳过它。 (有道理吗?)

我甚至不知道从哪里开始。我知道我需要一个循环,将文本文件添加到ArrayList,并检查一个单词是否在列表中。有什么想法吗?

PS:刚开始使用Java

这是我到目前为止所做的,甚至不知道我是否走在正确的道路上..

public String findWord(){
        int text = 0;
        int i = 0;
        while sc.hasNextLine()){
            wordArray[i] = sc.nextLine();
        }
            if wordArray[i].contains() {
        }
        i++;
    }

4 个答案:

答案 0 :(得分:4)

ListArrayList或其他)不是最好的数据结构; Set更好。在伪代码中:

  • 定义一个Set
  • 代表每个单词
    • 如果添加到集合返回false,请跳过它
    • 否则做任何想要做的事情(第一次遇到)字

Set true方法返回 $array = array("admin", "moderator", "user"); if ($array[0] == "user") { // CODE } ,如果该集合因调用而更改,只有当该单词不在该集合中时才会发生,因为设置不允许重复。

答案 1 :(得分:0)

我曾经做过一个类似的程序,它通过文本文件读取并计算出一个单词出现的次数。 我开始导入扫描程序,以及文件系统(这需要位于java类的顶部)

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.File;
import java.io.PrintStream;
import java.util.Scanner;

然后你可以制作文件,以及从这个文件中读取扫描仪,确保相应地调整文件的路径。新的Printstream不是必需的,但在处理大量数据时,我不喜欢溢出控制台。

public static void main(String[] args) throws FileNotFoundException {
    File file=new File("E:/Youtube analytics/input/input.txt");
    Scanner scanner = new Scanner(file); //will read from the file above
    PrintStream out = new PrintStream(new FileOutputStream("E:/Youtube      analytics/output/output.txt"));
    System.setOut(out);     
}

在此之后你可以使用scanner.next()来获取下一个单词,这样就可以这样写:

String[] array=new String[MaxAmountOfWords];//this will make an array
int numberOfWords=0;  
String currentWord="";

while(scanner.hasNext()){
  currentWord=scanner.next();

  if(isNotInArray(currentWord))
  {
      array[numberOfWords]=currentWord
  }
  numberOfWords++;
}

如果你不了解这一点或需要进一步指导进展,请告诉我。如果我们不确切知道你在哪里,很难帮助你......

答案 2 :(得分:0)

你可以试试这个:

public List<String> getAllWords(String filePath){    
        String line;
        List<String> allWords = new ArrayList<String>();
        BufferedReader reader = new BufferedReader(new FileReader(new File(filePath)));
        //read each line of the file
        while((line = reader.readLine()) != null) {
            //get each word in the line
            for(String word: line.split("(\\w)+"))
                //validate if the current word is not empty
                if(!word.isEmpty())
                    if(!allWords.contains(word))
                        allWords.add(word);
            }
        }
        return allWords;
    }

答案 3 :(得分:0)

最佳解决方案是使用Set。但是,如果您仍想使用List,请执行以下操作:

假设文件包含以下数据:

Hi how are you
I am Hardi
Who are you

代码将是:

    List<String> list = new ArrayList<>();

    // Get the file.
    FileInputStream fis = new FileInputStream("C:/Users/hdinesh/Desktop/samples.txt");

    //Construct BufferedReader from InputStreamReader
    BufferedReader br = new BufferedReader(new InputStreamReader(fis));

    String line = null;
    // Loop through each line in the file
    while ((line = br.readLine()) != null) {
        // Regex for finding just the words
        String[] strArray = line.split("[ ]");
        for (int i = 0; i< strArray.length; i++) {
            if (!list.contains(strArray[i])) {
                list.add(strArray[i]);
            }
        }
    }

    br.close();

    System.out.println(list.toString());

如果您的文本文件包含特殊字符的句子,则必须为此编写正则表达式。

相关问题