简体   繁体   English

Java计算字符在文件中出现的次数

[英]Java counting the number of times a character appears in a file

I am aware that this question may have been asked before, but after browsing through the other questions I couldn't find a solution for my problem. 我知道这个问题可能之前曾有人问过,但是在浏览了其他问题之后,我找不到解决我问题的方法。 So I am trying to count the number of times a certain character appears in a .txt file. 因此,我试图计算某个字符在.txt文件中出现的次数。 My code is able to count the number of times a character appears, but it is not ignoring the case of the letter so when it should output 15 'a' instead it does 13 'a' because there are two capital A's in my .txt file. 我的代码能够计算一个字符出现的次数,但是它不会忽略字母的大小写,因此当它应该输出15个“ a”而不是13个“ a”时,因为我的.txt中有两个大写的A文件。

Here is my code: 这是我的代码:

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
public class Program6 {
ArrayList<Integer> countOfLetters;
int count(String filename, char ch)
{
System.out.println(ch); 
File fin = new File(filename);      
    Scanner scan = null;    
    try{

    }
    catch(Exception e)
    {

    }
    return 0;
}   
void writeCountResult ( String filename )
{
    File fin = new File(filename);      
}
public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    Program6 program6 = new Program6();
    String filename = "xanadu.txt";
    program6.countOfLetters = new ArrayList<Integer>();
    for(int i=65;i<91;i++)
    {
        int num = program6.count(filename,(char)i);
        program6.countOfLetters.add(num);
    }
    program6.writeCountResult("output.txt");

    //This code counts the number of times "a" appears in the file but  not "A" BufferedReader reader = new BufferedReader(new                  FileReader("xanadu.txt"));
        int ch;
        char charToSearch='a';      
        int counter=0;
        while((ch=reader.read()) != -1) {
            if(charToSearch == (char)ch) {
                counter++;
            }
        };
        reader.close();

        System.out.println(counter); 
 }

}

Part 2: Now write another method to write the counts of any three of the above letters in a file called "output.txt". 第2部分:现在编写另一种方法,将上述三个字母中任何三个的计数写入一个名为“ output.txt”的文件中。 Use the following method header:void writeCountResult ( String filename ). 使用以下方法header:void writeCountResult(String filename)。 As you can see this is already in my code, but I was not sure where to go next to count the characters to an output.txt from the other txt file. 如您所见,这已经在我的代码中了,但是我不确定下一步该如何将其他txt文件中的字符计数到output.txt中。

modify your if statement to check if the charToSearch is equal to the upper case version of the character. 修改if语句,以检查charToSearch是否等于字符的大写版本。

if (charToSearch == Character.toLowerCase((char)ch) || charToSearch == Character.toUpperCase((char)ch) {
                counter++;
}

chIn is the char variable which u want to search. chIn是您要搜索的char变量。

 int asci;     
 asci = br.read();
 if(asci >=65 && asci <= 90)
    asci += 32;
if (chIn >= 65 && chIn<=90)
    chIn+= 32;

if(asci == chIn)
   count++;

you can try the above logic for counting upper as well as lower case chars. 您可以尝试使用上述逻辑来计算大小写字符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM