java - 列出名称和出现次数

标签 java arrays

所以任务是读取具有以下名称的文件:

Alice
Bob 
James  
Richard  
Bob  
Alice  
Alice  
Alice  
James  
Richard  
Bob 
Richard  
Bob  
Stephan  
Michael  
Henry   

并打印出每个名称及其出现的值,例如“Alice - <4>”。
基本上我已经成功了。我唯一的问题是我的输出中缺少姓氏(Stephan - <1>),我无法让它正常工作。这可能是因为我使用了 [i-1] 但正如我所说,我我在这里没有得到正确的解决方案。
好吧,这是我的代码..

package Assignment4;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Arrays;

public class ReportUniqueNames {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println ("       This programm counts words, characters and lines!\n");
        System.out.println ("Please enter the name of the .txt file:");

        BufferedReader input = new BufferedReader(new InputStreamReader (System.in));

        BufferedReader read = null;
        String file = "";
        String text = "";
        String line = "";
        boolean unique = true;
        int nameCounter = 1;        

        try {

            file = input.readLine();
            read = new BufferedReader (new FileReader(file));
            while ((line = read.readLine()) != null) {
                text += line.trim() + " ";                  
            }

        } catch (FileNotFoundException e) {
            System.out.println("File was not found.");          
        } catch (IOException e) {
            System.out.println("An error has occured.");            
        }

        String textarray[] = text.split(" ");
        Arrays.sort(textarray);

        for (int i=0; i < textarray.length; i++) {

            if (i > 0 && textarray[i].equals(textarray[i-1])) {
                nameCounter++;
                unique = false;
            }

            if (i > 0 && !textarray[i].equals(textarray[i-1]) && !unique) {
                    System.out.println("<"+textarray[i-1]+"> - <"+nameCounter+">");
                    nameCounter = 1;
                    unique = true;
            } else if (i > 0 && !textarray[i].equals(textarray[i-1]) && unique) {
                //nameCounter = 1;
                System.out.println("<"+textarray[i-1]+"> - <"+nameCounter+">");
            }           

        }

    }

}

就是这样..希望你们中的一个人可以帮助我。

编辑:
哇,有这么多不同的方法。
首先感谢您的帮助。
我会仔细查看您建议的解决方案,也许会从头开始;)。
完成后会给您提示。

最佳答案

您可以简单地使用Map(模拟“Multiset ”)来计算单词数:

String textarray[] = text.split(" ");

// TreeMap gives sorting by alphabetical order "for free"
Map<String, Integer> wordCounts = new TreeMap<>();

for (int i = 0; i < textarray.length; i++) {
    Integer count = wordCounts.get(textarray[i]);
    wordCounts.put(textarray[i], count != null ? count + 1 : 1);
}

for (Map.Entry<String, Integer> e : wordCounts.entrySet()) {
    System.out.println("<" + e.getKey() + "> - <" + e.getValue() + ">");
}

关于java - 列出名称和出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30551616/

相关文章:

arrays - Swift - 访问 nil 不会崩溃

javascript - 如何从对象数组中获取数组本身属性的所有唯一值

JavaScript 不区分大小写的数组通用排序

Java - Swing Layout + AWT - 如何在使用布局定位的 JPanel 中绘制一条线?

Java AWT 应用程序窗口填充

java - @Resource 为 String 类的 bean 返回 null

java - 缺少 org.apache.tomcat.jdbc.pool.DataSource

javascript - 每秒更改文本颜色

java - 尝试将数组转换为文本文件

java - IntelliJ IDEA Community Edition 和 Spring 使用 Spring DevTools 缓慢重新加载