保留文本文件中字母出现的计数吗?

时间:2019-04-02 01:09:34

标签: java

我必须编写一个程序,其中必须找到文件“ test.txt”中每个字母的多少,然后将该数字除以字母总数,以得出每个字母的出现百分比(我还没有完成)。但是,我什至不知道如何查找文本文件中每个字母的编号。这是我极其失败的尝试。谁能帮我。

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) {}
}
}

2 个答案:

答案 0 :(得分:0)

我采取了另一种方法。请参阅下面的代码和代码注释以获取解释。

代码:

package main;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DecimalFormat;

public class Main {

    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' };
            double percentage[] = new double[26]; // declare a variable that will hold the percentages of each letter
            String path = "sample.txt";
            // Read all the contents of the file
            String text = new String(Files.readAllBytes(Paths.get(path)), StandardCharsets.UTF_8);
            // make a temporary copy of the content without all the letters
            String allLetters = text.replaceAll("[a-z]+", ""); //
            // Get the total number of characters in the file
            // by subtracting the new length (the length after all the letter has been
            // deleted) from the original length
            int totalNumOfLetters = text.length() - allLetters.length();

            // convert the contents into lower-case
            text = text.toLowerCase();
            for (int i = 0; i < letters.length; i++) {
                char c = letters[i];
                // make a temporary copy of the content without the current letter
                String tmp = text.replaceAll(c + "", "");

                // subtract the new length (the length after current letter has been deleted)
                // from the original length
                int count = text.length() - tmp.length();

                // get the percentage of the occurrence of the current letter
                percentage[i] = (double) count / (double) totalNumOfLetters * 100.00;
            }
            DecimalFormat df2 = new DecimalFormat(".##"); // for formatting only
            for (int i = 0; i < percentage.length; i++) {
                System.out.println("\"" + letters[i] + "\" has a percentage of " + df2.format(percentage[i]) + "%");
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

示例文件:

'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'
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 

示例输出:

"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%

答案 1 :(得分:0)

仅当该文件上的单词在 一行 上时,此方法才有效,但是可以使用空格。如果您仍然有问题,我明天也许可以解决此问题,但是明天我有课,所以我得睡觉了。希望这会有所帮助!

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------------------------------------------------");
        }
    }
}

}

test.txt文件包括:

萨拉姆男子亚当男子亚当萨拉姆

输出:

字符“ s”的出现次数是:2

字符“ s”的出现百分比为:0.083


字符“ a”的出现是:8

字符“ a”的出现百分比是:0.333


字符“ l”的出现是:2

字符“ l”的出现百分比是:0.083


字符“ m”的出现是:6

字符“ m”的出现百分比是:0.250


字符“ e”的出现是:2

字符“ e”的出现百分比是:0.083


字符“ n”的出现是:2

字符“ n”的出现百分比是:0.083


字符“ d”的出现是:2

字符“ d”的出现百分比是:0.083

相关问题