Java 是否保留文本文件中字母出现的次数?
我必须编写一个程序,在这个程序中,我必须找到“test.txt”文件中每个字母的数量,然后将这个数字除以字母总数,找到每个字母的出现百分比(我还没有做到)。然而,我甚至不知道如何找到文本文件中每个字母的编号。这是我极其失败的尝试。谁能帮帮我吗Java 是否保留文本文件中字母出现的次数?,java,Java,我必须编写一个程序,在这个程序中,我必须找到“test.txt”文件中每个字母的数量,然后将这个数字除以字母总数,找到每个字母的出现百分比(我还没有做到)。然而,我甚至不知道如何找到文本文件中每个字母的编号。这是我极其失败的尝试。谁能帮帮我吗 import java.io.*; class EnglishAnalysis { public static void main(String[] args) { try { char letters[] = {'a','b','c','d','e'
import java.io.*;
class EnglishAnalysis
{
public static void main(String[] args)
{
try
{
char letters[] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
FileReader fr = new FileReader("test.txt");
BufferedReader br = new BufferedReader(fr);
int count = 0;
int totalLetters = 0;
String lineCharacters [];
String line;
line = br.readLine();
while (line != null)
{
lineCharacters = line.split("");
for (int i = 0; i < lineCharacters.length; i++)
{
if (!Character.isWhitespace(line.charAt(i)))
totalLetters++;
for (int j = 0; j <= i; j++)
{
if (line.charAt(i) == letters[j])
count++;
System.out.println(line.charAt(i) + " : " + count);
}
}
line = br.readLine();
}
System.out.println(totalLetters);
br.close();
}
catch (IOException e) {}
}
}
import java.io.*;
课堂英语分析
{
公共静态void main(字符串[]args)
{
尝试
{
字符字母[]={'a'、'b'、'c'、'd'、'e'、'f'、'g'、'h'、'i'、'j'、'k'、'l'、'm'、'n'、'o'、'p'、'q'、'r'、's'、't'、'u'、'v'、'w'、'x'、'y'、'z';
FileReader fr=新的FileReader(“test.txt”);
BufferedReader br=新的BufferedReader(fr);
整数计数=0;
整数=0;
字符串行字符[];
弦线;
line=br.readLine();
while(行!=null)
{
lineCharacters=行。拆分(“”);
对于(int i=0;i 对于(int j=0;j我采用了不同的方法。请参阅下面的代码和代码注释以了解解释
代码:
样本输出:
"a" has a percentage of 5.88%
"b" has a percentage of 5.88%
"c" has a percentage of 5.88%
"d" has a percentage of 5.88%
"e" has a percentage of 5.88%
"f" has a percentage of 5.88%
"g" has a percentage of 5.88%
"h" has a percentage of 5.88%
"i" has a percentage of 2.94%
"j" has a percentage of 2.94%
"k" has a percentage of 2.94%
"l" has a percentage of 2.94%
"m" has a percentage of 2.94%
"n" has a percentage of 2.94%
"o" has a percentage of 2.94%
"p" has a percentage of 2.94%
"q" has a percentage of 2.94%
"r" has a percentage of 2.94%
"s" has a percentage of 2.94%
"t" has a percentage of 2.94%
"u" has a percentage of 2.94%
"v" has a percentage of 2.94%
"w" has a percentage of 2.94%
"x" has a percentage of 2.94%
"y" has a percentage of 2.94%
"z" has a percentage of 2.94%
这只适用于文件中的单词在一行的情况,但可以使用空格。如果你仍然有问题,我可能明天就能解决这个问题,但我明天有课,所以我必须去睡觉。希望这有帮助
import java.io.*;
import java.util.*;
public class CountLettersFromFile {
public static void main(String[] args) throws IOException {
FileReader file = new FileReader("test.txt");
BufferedReader br=new BufferedReader(file);
String[] lineCharacters;
String line;
line=br.readLine();
int totalLetters=0;
lineCharacters=line.split("");
for (int i = 0; i < lineCharacters.length; i++) {
if(!Character.isWhitespace(line.charAt(i)))
totalLetters++;
}
double[] count = new double[255];
int length = line.length();
for (int i = 0; i < length; i++) {
count[line.charAt(i)]++;
}
char[] ch = new char[line.length()];
for (int i = 0; i < length; i++) {
ch[i] = line.charAt(i);
int find = 0;
for (int j = 0; j <= i; j++) {
if (line.charAt(i) == ch[j])
find++;
}
if (find == 1 && !Character.isWhitespace(line.charAt(i))) {
System.out.printf("Occurance of character \"" + line.charAt(i) + "\" is: %.0f %n"
, count[line.charAt(i)]);
System.out.printf("Percent occurance of character \"" +line.charAt(i) + "\" is: %.3f %n"
, (count[line.charAt(i)])/totalLetters,"%");
System.out.println("\n------------------------------------------------");
}
}
}
import java.io.*;
导入java.util.*;
公共类CountLettersFromFile{
公共静态void main(字符串[]args)引发IOException{
FileReader文件=新的FileReader(“test.txt”);
BufferedReader br=新的BufferedReader(文件);
字符串[]行字符;
弦线;
line=br.readLine();
整数=0;
lineCharacters=行。拆分(“”);
对于(int i=0;i for(int j=0;j提示:每个字符都是一个int。通过一点数学运算,您可以将a
转换为0
,b
转换为1
等等。然后,只需在数组中的该位置增加一个int即可(这是假设所有字母都是小写,就像在数组中一样)提示2:或者,您可以使用映射
(但数组是更有效的解决方案)。您的计数++;
全局用于所有字符,您可以按照Elliott的建议在映射上设置计数。一种可能的解决方案。
"a" has a percentage of 5.88%
"b" has a percentage of 5.88%
"c" has a percentage of 5.88%
"d" has a percentage of 5.88%
"e" has a percentage of 5.88%
"f" has a percentage of 5.88%
"g" has a percentage of 5.88%
"h" has a percentage of 5.88%
"i" has a percentage of 2.94%
"j" has a percentage of 2.94%
"k" has a percentage of 2.94%
"l" has a percentage of 2.94%
"m" has a percentage of 2.94%
"n" has a percentage of 2.94%
"o" has a percentage of 2.94%
"p" has a percentage of 2.94%
"q" has a percentage of 2.94%
"r" has a percentage of 2.94%
"s" has a percentage of 2.94%
"t" has a percentage of 2.94%
"u" has a percentage of 2.94%
"v" has a percentage of 2.94%
"w" has a percentage of 2.94%
"x" has a percentage of 2.94%
"y" has a percentage of 2.94%
"z" has a percentage of 2.94%
import java.io.*;
import java.util.*;
public class CountLettersFromFile {
public static void main(String[] args) throws IOException {
FileReader file = new FileReader("test.txt");
BufferedReader br=new BufferedReader(file);
String[] lineCharacters;
String line;
line=br.readLine();
int totalLetters=0;
lineCharacters=line.split("");
for (int i = 0; i < lineCharacters.length; i++) {
if(!Character.isWhitespace(line.charAt(i)))
totalLetters++;
}
double[] count = new double[255];
int length = line.length();
for (int i = 0; i < length; i++) {
count[line.charAt(i)]++;
}
char[] ch = new char[line.length()];
for (int i = 0; i < length; i++) {
ch[i] = line.charAt(i);
int find = 0;
for (int j = 0; j <= i; j++) {
if (line.charAt(i) == ch[j])
find++;
}
if (find == 1 && !Character.isWhitespace(line.charAt(i))) {
System.out.printf("Occurance of character \"" + line.charAt(i) + "\" is: %.0f %n"
, count[line.charAt(i)]);
System.out.printf("Percent occurance of character \"" +line.charAt(i) + "\" is: %.3f %n"
, (count[line.charAt(i)])/totalLetters,"%");
System.out.println("\n------------------------------------------------");
}
}
}