gpt4 book ai didi

java - 程序将点(.)计为字母字符

转载 作者:太空宇宙 更新时间:2023-11-04 10:25:24 25 4
gpt4 key购买 nike

所以我的任务是创建一个程序,将文件作为输入并计算其中每个字母字符的出现次数。然后我将打印这封信、它出现的次数和频率。我让它几乎按计划工作。我遇到的唯一问题是,当我打印时,它还会打印文件中的点数(.)。我无法阻止它这样做。请帮忙..

public class CountOccurences {
private static Scanner input;

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

DecimalFormat dec = new DecimalFormat("#.000");
input = new Scanner(new File("story.txt"));

int[] ltrCtr = new int[127]; // This array counts the number of occurences for every letter / symbol on the ascii table.

String str = "";
// Puts the textfile as a String
while(input.hasNext()) {
str += input.next();
}

char[] text = str.toCharArray();

char temp; int tempInt;
int ctr = 0;

for(int i = 0; i < text.length; i++) { // Loops through the text

temp = text[i]; // Gets the char at i
tempInt = (int)temp; // Get the ascii value of the char at i

ltrCtr[tempInt]++;

if(Character.isAlphabetic(text[i])) {
ctr++;
}
}
System.out.println("Letter" + " Amount" + " Freq");
for(int i = 0; i < ltrCtr.length; i++) {
if(ltrCtr[i] >= 1 && (int)ltrCtr[i] != 46) {
System.out.println(" " + (char)i + " " +
ltrCtr[i] + " " +
dec.format((double)ltrCtr[i]/ctr) + "%");

}
}

input.close();

}

}

最佳答案

我相信您打算使用 isLetter ,不是isAlphabetic .

关于java - 程序将点(.)计为字母字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50589316/

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