在多行字符串数组中查找倒数第二个单词

时间:2015-06-23 01:29:37

标签: java string

我需要在每一行中找到倒数第二个单词(它们按空格划分)并找到3个最受欢迎的单词并查找有多少个?你能以任何方式帮助我吗?

输入示例:

abcd i
asd ffdds abcd ddd ?
abcd ffdds asd ddd i
ddd abcd i
a f g w e a asdfasdasdas fdd i

我需要的答案:

abcd 2
ddd 2
fdd 1

2 abcd
2 ddd
1 fdd

这是我的代码

public class asdf {

    public static void main(String args[]) throws IOException {
        BufferedReader in = new BufferedReader(new FileReader("input.txt"));
        String str;

        List < String > list = new ArrayList < String > ();
        while ((str = in .readLine()) != null) {
            if (str.startsWith(" ") && str.endsWith("i") || str.endsWith("?")) {

                list.add(str);
            }
        }
        String[] stringArr = list.toArray(new String[0]); //for backup
        String[] stringArrAC = list.toArray(new String[0]);

        for (int i = 0; i < stringArrAC.length; i++) {
            stringArrAC[i] = stringArrAC[i].substring(63);
        }
        //String[] stringArrLAST = (new String[0]);
         Map<String, Integer> occurrences = new HashMap();
        for (String line : stringArrAC) {
            String[] words = line.split(" ");
            String nextToLastWord = words[words.length - 2];
            occurrences.put(nextToLastWord, 
                    occurrences.get(nextToLastWord) == null 
                            ? 1 
                            : occurrences.get(nextToLastWord) + 1); 
        }

        occurrences.entrySet().stream()
            // Sort the values in descending order
            .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
            // Gets top 3 entries
            .limit(3)
            // Print them 
            .forEach(System.out::println);
        try {
            PrintWriter pr = new PrintWriter("output.txt");
            for (int i = 0; i < stringArrAC.length; i++) {
                pr.println(stringArrAC[i]);
            }
            pr.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("No such file exists.");
        }
    }

2 个答案:

答案 0 :(得分:0)

使用字符串标记生成器解析每一行以获取单词数组。对于每一行,您想要的单词将是数组中倒数第二个元素。创建一个Map来存储单词,以及一个相关的计数器,每当你遇到同一个单词时,你将增加一个。换句话说,一旦单词出现在地图中,如果再次找到该单词,则在地图中增加其计数器。完成后,从地图中获取键值对,找到3个最高的计数器值及其相关的单词。

答案 1 :(得分:0)

Java 8使这更简单 使用HashMap计算倒数第二个单词的出现次数,然后使用流按降序对HashMap进行排序并获取前三个值。

public static void main(String[] args) throws Exception {
    List<String> lines = new ArrayList() {
        {
            add("abcd i");
            add("asd ffdds abcd ddd ?");
            add("abcd ffdds asd ddd i");
            add("ddd abcd i");
            add("a f g w e a asdfasdasdas fdd i");
            add("123 awef bad");
            add("123 awef bad");
            add("123 awef bad");
            add("oneword");
        }
    };

    Map<String, Integer> occurrences = new HashMap();
    for (String line : lines) {
        // Skip blank lines
        if (line.isEmpty()) {
            continue;
        }

        String[] words = line.split(" ");
        // Account for a line that might have only one word
        String nextToLastWord = words.length >= 2 ? words[words.length - 2] : words[0];
        occurrences.put(nextToLastWord,
                occurrences.get(nextToLastWord) == null
                        ? 1
                        : occurrences.get(nextToLastWord) + 1);
    }

    occurrences.entrySet().stream()
            // Sort the values in descending order
            .sorted(Collections.reverseOrder(Map.Entry.comparingByValue()))
            // Gets top 3 entries
            .limit(3)
            // Print them 
            .forEach(System.out::println);
}

结果:

awef=3
ddd=2
abcd=2