读取文本文件,然后执行字符计数并打印每个文件的相对频率 [英] Reading a text file then performing a character count and printing the relative frequency of each one

查看:161
本文介绍了读取文本文件,然后执行字符计数并打印每个文件的相对频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个文本文件的字符数,然后显示每个与它的相对频率,其余的字符,但我目前只是空白控制台回来。任何帮助将不胜感激。

  import java.io. *; 

$ b public class RelativeFrequency {

public static void main(String [] args)throws IOException {

文件file1 = new File ( Rf.txt);
BufferedReader in = new BufferedReader(new FileReader(file1));
System.out.println(Letter Frequency);

int nextChar;
char ch;

int [] count = new int [26]; ((nextChar = in.read())!= -1){
ch =((char)nextChar);


if(ch> ='a'&& ch< ='z')
count [ch - 'a'] ++;


$ b for(int i = 0; i <26; i ++){
System.out.printf(,i +'A' ,count [i]);

}



in.close();




$ b

解决方案

您的printf语句未正确格式化

  System.out.printf(%c%d ,i +'A',count [i]); 


I'm looking to perform a character count on a text file and then display each one with it's relative frequency to the rest of the characters, but I'm presently getting just blank console back. Any help would be greatly appreciated.

import java.io.*;


public class RelativeFrequency {

  public static void main(String[] args) throws IOException {

       File file1 = new File("Rf.txt");
       BufferedReader in = new BufferedReader (new FileReader (file1));
           System.out.println("Letter Frequency");

        int nextChar;
        char ch;

        int[] count = new int[26];

        while ((nextChar = in.read()) != -1) {
          ch = ((char) nextChar);
          if (ch >= 'a' && ch <= 'z')
          count[ch - 'a']++;
        }


        for (int i = 0; i < 26; i++) {
          System.out.printf("", i + 'A', count[i]);

        }



in.close();

}

}

解决方案

Your printf statement isn't formatted correctly

System.out.printf("%c %d", i + 'A', count[i]);

这篇关于读取文本文件,然后执行字符计数并打印每个文件的相对频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆