如何使用Java从文本文件中删除重复的单词

时间:2019-05-30 15:21:03

标签: java

我有一个类似的输入文件

names.txt

   Maria Derek Erica
   Livia Jack Anita
   Kendall Maria Livia Derek
   Jamie Jack
   Thomson Erica

我想输出类似。从名称中删除重复的单词

output.txt

   Maria Derek Erica
   Livia Jack Anita
   Kendall 
   Jamie 
   Thomson

我已经尝试读取用空格分隔的文件,然后将它们添加到ArrayList中,然后丢失了下一步要生成输出的内容。

import java.util.Scanner;
import java.io.*;
import java.util.ArrayList;

public class duplicate {
   public static void main(String[] args) throws IOException {
      ArrayList<String> list = new ArrayList<String>();
      File file = new File("weather.txt");
      Scanner input = new Scanner(file); 
      String prev = input.next();
      int count = 0;
      while (input.hasNext()) {
         String next  = input.next();
         System.out.println(next);
         set.add(next);
         count = count + 1;
         if(prev.equals(next))
         {
            System.out.println("Match found: -" + prev);
         }
         prev = next;
      } 

      System.out.println(list);
      System.out.println("Word count: " + count);
   }
}

4 个答案:

答案 0 :(得分:3)

您甚至可以将订单创建集保存为:

Set<String> uniqueNames = new LinkedHashSet<>();

然后,您可以将元素添加到集合中:

uniqueNames.add(next);

答案 1 :(得分:2)

如果顺序无关紧要,只需使用Set。 Set将自动过滤掉重复的元素。然后只需打印出该组即可。您会没事的。

Set<String> list = new HashSet<String>();

答案 2 :(得分:1)

import java.util.Scanner;
import java.io.*;
import java.util.HashSet;

public class Duplicate {
    public static void main(String[] args) throws IOException {
        HashSet<String> lines = new HashSet<String>();
        File file = new File("weather.txt");
        Scanner input = new Scanner(file);
        int count = 0;
        while (input.hasNext()) {
            String next  = input.next();
            System.out.println(next);
            lines.add(next);
            count++;
        } 
        input.close();
        System.out.println(lines);
        System.out.println("Word count: " + count);
        System.out.println("Unique word count: " + lines.size());
    }
}

答案 3 :(得分:0)

您将要使用Set或HashSet来跟踪重复的名称,并使用ArrayList来存储要写入输出文件的最终行。

由于我们要逐行进行更改,因此我们需要1)逐行读取文件,2)拆分行以处理各个名称,3)编辑行或创建根据需要添加一条新的结果行,然后4)然后将编辑后的行存储在末尾以输出:

import java.util.*;
import java.io.*;

public class Duplicate {
    public static void main(String[] args) throws IOException {
        HashSet<String> uniqueNames = new HashSet<String>();
        ArrayList<String> outputLines = new ArrayList<String>();

        Scanner input = new Scanner(new File("input.txt"));
        while (input.hasNextLine()) {

            // Split a line into an array of names.
            String[] names = input.nextLine().split(" ");
            String edited = "";
            for (int i = 0; i < names.length; i++) {

                // If the name is already in the set, remove it.
                if (uniqueNames.add(names[i])) {
                    edited += names[i] + " ";
                }
            }

            edited = edited.strip();  // remove excess whitespace

            // Add the final line to our output array.
            if (!edited.equals("")) {
                outputLines.add(edited);
            }
        }

        // Write the output array to a file.
        String outputFn = "output.txt";
        BufferedWriter output = new BufferedWriter(new FileWriter(outputFn));
        output.write(String.join("\n", outputLines));
        output.close();
        System.out.println("File '" + outputFn + "' created!");
    }
}

如果您想添加其他功能(例如计算总单词或唯一单词),则将其保留为练习,因为问题似乎主要与删除重复行为有关。

相关问题