我需要打印文本文件中每个字母的频率。现在,我对如何将字母放入数组中感到困惑,因此它们用数字表示(a-0,b-1,c-2,d-3等)。那我该如何计算每个字母而又不单独计算每个字母。
样本输入(在文本文件中):
我过得愉快。
样本输出(不包括省略号):
a-3 b-0 c-0 d-3 ... 1一 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
}
}
答案 0 :(得分:1)
仅此而已。该算法的复杂度应与文件中字母的数量成线性比例。
答案 1 :(得分:0)
读取文件后只有一个字符串,那么您可以使用以下代码轻松计算频率
Map<Character, Long> freq = Arrays.stream(arr).
collect(Collectors.groupingBy(Character::charValue, Collectors.counting()));