java - 计算文本文件中字母的出现次数

标签 java arrays algorithm

我需要打印文本文件中每个字母的频率。现在我对如何将字母放入数组中以便用数字(a-0、b-1、c-2、d-3 等)表示感到困惑。那么如何在不单独计算每个字母的情况下对每个字母进行计数。

示例输入(在文本文件中):

我度过了愉快的一天。

示例输出(不包括省略号):

a-3 b-0 c-0 d-3 ... g-1 h-1 i-1 ... o-2 ... y-1 z-0

/*
 * program that reads in a text file and counts the frequency of each letter
 * displays the frequencies in descending order
 */

import java.util.*; //needed for Scanner
import java.io.*;  //needed for File related classes
public class LetterCounter {
  public static void main(String args[]) throws IOException{
    Scanner keyboard = new Scanner(System.in); //Scanner to read in file name
    System.out.println("Enter the name of the text file to read:");
    String filename = keyboard.next();

    //This String has all the letters of the alphabet
    //You can use it to "look up" a character using alphabet.indexOf(...) to see what letter it is
    //0 would indicate 'a', 1 for 'b', and so on.  -1 would mean the character is not a letter
    String alphabet = "abcdefghijklmnopqrstuvwxyz";

    //TODO: create a way to keep track of the letter counts
    //I recommend an array of 26 int values, one for each letter, so 0 would be for 'a', 1 for 'b', etc.


    Scanner fileScan = new Scanner(new File(filename));  //another Scanner to open and read the file
    //loop to read file line-by-line
    while (fileScan.hasNext()) {  //this will continue to the end of the file
      String line = fileScan.nextLine();  //get the next line of text and store it in a temporary String
      line = line.toLowerCase( ); // convert to lowercase

      //TODO: count the letters in the current line


    }
    fileScan.close(); //done with file reading...close the Scanner so the file is "closed"



    //print out frequencies
    System.out.println("Letters - Frequencies in file:");

    //TODO: print out all the letter counts


  }
}

最佳答案

  1. 将所有字母转换为大写或小写
  2. 将它们放入哈希表中,以字母为键,以计数为值。

这就是全部内容了。算法的复杂度应该与文件中字母的数量成线性比例。

关于java - 计算文本文件中字母的出现次数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58758642/

相关文章:

java - 命名不同内容的最佳实践(Ex :String and byte) which has similar reference(Ex:name)

java - 调用一个类的构造函数来修改它以在另一个类中使用

在C中将ASCII码转换为字符串

c++ - 使用单调多边形的多边形三角剖分

python - 确定线段集合的非凸包

java - 局部变量初始化 null 和不初始化之间的区别?

java - 不包含4级-Java

arrays - 在VBA中用零替换异常值

c - 从C中的字符串中获取子字符串

algorithm - 图灵机设计0和1