Java中的字母频率程序

Java中的字母频率程序,java,Java,我正在尝试编写一个字母频率程序,该程序对.txt文件中的字母字符进行计数,并在一个2列表格中显示频率数据。我不知道如何从display函数调用函数printChars。我还在printChars功能中打印频率数据。有人有什么建议吗?谢谢 这是我的密码: final static int AlphabetSize = 26; final static Scanner cin = new Scanner(System.in); final static PrintStream cou

我正在尝试编写一个字母频率程序,该程序对.txt文件中的字母字符进行计数,并在一个2列表格中显示频率数据。我不知道如何从display函数调用函数
printChars
。我还在
printChars
功能中打印频率数据。有人有什么建议吗?谢谢

这是我的密码:

        final static int AlphabetSize = 26;
final static Scanner cin = new Scanner(System.in);
final static PrintStream cout = System.out;
final static int MaxBarLength = 50;

public static void main(String[] args) {
    String fileName;




    // get the file name
    cout.print("Enter the file name: ");
    fileName = cin.nextLine();

    // process the file
    try {
        processFile(fileName);
    }
    catch (IOException e) {
        e.printStackTrace();
    } // end try



} // end main

static void processFile(final String fileName) 
        throws FileNotFoundException, IOException 
{
    FileInputStream inFile = new FileInputStream(fileName);
    int inputValue;

    // declare other variables you need
            int counters [] = new int [26];

    // get the first character from file
    inputValue = inFile.read();
    while (inputValue != -1) {
        char ch = (char) inputValue;

        // add code to process this character
                   if (ch >= 'a' && ch <= 'z'){
                       counters[ch - 'a']++;
                   }

        // read next input character
        inputValue = inFile.read();
    } // end loop

    inFile.close();

    // generate appropriate output
            display(counters);

} // end function

static void display(final int [] counters) {
    // write code for this function

    System.out.println("Letter" + " " + "Count");
    System.out.println("------" + " " + "-----");
   printChars(n, c);
   } // end function

// char2int is complete
static int char2int(final char arg) {
    if (!Character.isLetter(arg))
        return -1;
    else
        return (int) Character.toUpperCase(arg) - (int) 'A';
} // end function


// function printChars writes n copies of the character c to the
// standard output device
static void printChars (final int n, final char c) {
    // write the code
    for (int i = 0; i < 26; i++){
       System.out.printf("%c%7d\n", i + 'A', counters[i]);
   }

           }

 // end printChars
final static int alphabetize=26;
最终静态扫描仪cin=新扫描仪(System.in);
最终静态打印流cout=System.out;
最终静态int MaxBarLength=50;
公共静态void main(字符串[]args){
字符串文件名;
//获取文件名
cout.print(“输入文件名:”);
fileName=cin.nextLine();
//处理文件
试一试{
进程文件(文件名);
}
捕获(IOE异常){
e、 printStackTrace();
}//结束尝试
}//末端总管
静态void processFile(最终字符串文件名)
抛出FileNotFoundException,IOException
{
FileInputStream inFile=新的FileInputStream(文件名);
int输入值;
//声明您需要的其他变量
整数计数器[]=新整数[26];
//从文件中获取第一个字符
inputValue=infle.read();
while(输入值!=-1){
char ch=(char)inputValue;
//添加代码以处理此字符

如果(ch>='a'&&ch您需要更改
printChars
的签名以获取计数器数组:

static void printChars (final int[] counters) {
    for (int i = 0; i < 26; i++){
       System.out.printf("%c%7d\n", i + 'A', counters[i]);
   }
}

您需要更改
printChars
的签名以获取计数器数组:

static void printChars (final int[] counters) {
    for (int i = 0; i < 26; i++){
       System.out.printf("%c%7d\n", i + 'A', counters[i]);
   }
}

如果我要在processFile函数中按如下方式更改代码。我如何调用char2int函数。当我尝试更改时,NetBeans是红线批注的。@dasblinkenlight'If(inputValue!=-1){char2int(arg);alpha++;}else{num++;}@WilliamWhite您对
char2int
的调用应该可以工作,只要您向它传递
char
参数。例如,
计数器[char2int(ch)]++;
就可以工作()。如果我要在processFile函数中按如下方式更改代码。我如何调用char2int函数。当我尝试更改时,NetBeans是红线批注的。@dasblinkenlight'If(inputValue!=-1){char2int(arg);alpha++;}else{num++;}@WilliamWhite只要传递
char
参数,调用
char
就应该有效。例如,
计数器[char2int(ch)]++;
将有效()。