gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:19:59 26 4
gpt4 key购买 nike

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

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

我度过了愉快的一天。

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

a-3b-0c-0d-3...g-1h-1i-1...o-2...y-1z-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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com