gpt4 book ai didi

java - 如何用java计算pdf文件中的字母数量

转载 作者:行者123 更新时间:2023-12-02 10:33:54 25 4
gpt4 key购买 nike

在这个程序中,我需要计算所有字母表(a-z)的数量,下面的代码只能计算“u”的数量。那么如果我想数到26,需要在for循环中写26个if语句吗?

输出应该是:答:389乙:777密码:909....

请给我其他解决方案

            String[] words = sourceCode.split(" ");
amountOfWords = amountOfWords + words.length;
for (String word : words) {
amountOfChars = amountOfChars + word.length();

for (int i = 0; i < word.length(); i++) {
if (word.charAt(i) == 'u' || word.charAt(i) == 'U') {
u++;
}
}
}

最佳答案

你可以试试这个。我正在使用库:fontbox-2.0.12pdfbox-2.0.12commons-logging-1.2

        try {
PDDocument doc = PDDocument.load(new File("E:\\project-test\\scloud\\test\\src\\main\\resources\\Shadip_Banik.pdf"));
String text = new PDFTextStripper().getText(doc);

sourceCode = text.replace("-", "").replace(".", "");


String[] words = sourceCode.split(" ");

amountOfWords = amountOfWords + words.length;
StringBuilder builder = new StringBuilder();

for (String word : words) {
amountOfChars = amountOfChars + word.length();

Pattern pattern = Pattern.compile("[a-zA-Z]");
Matcher matcher = pattern.matcher(word);
//System.out.println("MatchesCount "+matcher.group());
while (matcher.find()) {
builder.append(matcher.group());

}
}
String allData = builder.toString();
System.out.println(builder.toString());
int total = 0;
for (int i=0;i<allAlphabate.length();i++)
{
int alphabateCount = 0;
Pattern pattern = Pattern.compile(Character.toString(allAlphabate.charAt(i)));
Matcher matcher = pattern.matcher(allData);
while (matcher.find()) {
alphabateCount++;
}
total+=alphabateCount;
System.out.println(allAlphabate.charAt(i) +" : "+alphabateCount);
}
if(total == builder.toString().length())
{
System.out.println("Yes -------------------------------------------------");
}
System.out.println("Amount of Chars is " + amountOfChars);
System.out.println("Amount of Words is " + (amountOfWords + 1));
System.out.println("Average Word Length is " + (amountOfChars / amountOfWords));
} catch (Exception ert) {

}

但是如果你想一起计算大写和小写,请使用此代码

     for (String word : words) {
amountOfChars = amountOfChars + word.length();

Pattern pattern = Pattern.compile("[a-zA-Z]");
Matcher matcher = pattern.matcher(word.toLowerCase());
//System.out.println("MatchesCount "+matcher.group());
while (matcher.find()) {
builder.append(matcher.group());

}
}
String allData = builder.toString();
int total = 0;
for (int i=0;i<allAlphabate.length();i++)
{
int alphabateCount = 0;
Pattern pattern = Pattern.compile(Character.toString(allAlphabate.charAt(i)));
Matcher matcher = pattern.matcher(allData.toLowerCase());
while (matcher.find()) {
alphabateCount++;
}
total+=alphabateCount;
System.out.println(allAlphabate.charAt(i) +" : "+alphabateCount);
}
if(total == builder.toString().length())
{
System.out.println("Yes -------------------------------------------------");
}

关于java - 如何用java计算pdf文件中的字母数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53442215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com