所以我在下面有一段代码加载到文本文件中并对其进行分析,以显示存储在文本文件中的单词或句子中每个字母的百分比。
我希望能够使用char []
代码,而不是使用ASCII
个字母。
我不确定如何去做,所以任何帮助都会受到赞赏!
这是我到目前为止的代码:
import java.io.File;
import java.util.Scanner;
public class testing {
@SuppressWarnings("resource")
public static void main(String[] args){
Scanner scan;
try {
scan = new Scanner(new File("G:/test.txt")); //change directory to load in text file
}
catch (Exception e) {
System.out.println("File was not found ");
return;
}
int[] count = new int[26];
int total = 0;
while(scan.hasNextLine()) {
String text2 = scan.nextLine();
System.out.println ("\n--------------------------------------------------------------------------------------------------------");
System.out.println (" Words loaded in from the file: " + text2);
System.out.println ("--------------------------------------------------------------------------------------------------------");
System.out.print("\n");
System.out.print("");
char[] letters = text2.toCharArray();
char[] letters1 = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n','o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
for(int i = 0; i < letters.length; i++) {
for(int j = 0; j < 26; j++) {
if(letters[i] == letters1[j]) {
count[j]++;
total = total + 1;
break;
}
}
}
for (int i = 0; i < 26; i++){
System.out.format("| "+ letters1[i]+" ");
}
System.out.println ("\n----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
for (int i = 0; i < 26; i++){
float percentage = 0;
if (count[i] > 0)
percentage = ((float) count[i]/total)*100;
System.out.format("| "+percentage+ "%% ");
}
}
}
}
答案 0 :(得分:2)
Java中的char
只是一个无符号的16位整数,包含与该字符关联的Unicode代码点(至少如果它在BMP中)。 Unicode是ASCII的超集,因此您的char[]
已经包含了您正在处理的字符的ASCII值。
答案 1 :(得分:0)
char
可以转换为int
(0-127)(see here),因此很容易将其保存在数组中。您只需编辑代码中使用拉丁字母表的部分,并用更大的数组替换它。
这应该有效:
int[] count = new int[128];
int total = 0;
while (scan.hasNextLine()) {
String text2 = scan.nextLine();
System.out.println ("\n--------------------------------------------------------------------------------------------------------");
System.out.println (" Words loaded in from the file: " + text2);
System.out.println ("--------------------------------------------------------------------------------------------------------");
System.out.print("\n");
System.out.print("");
char[] letters = text2.toCharArray();
for (int i = 0; i < letters.length; i++) {
count[letters[i]]++;
total++;
}
System.out.println("\n----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
for (int i = 0; i < letters.length; i++) {
float percentage = 0;
if (count[1] > 0)
percentage = ((float) count[i] / total) * 100;
System.out.format("| " + percentage + "%% ");
}
}
我不知道以下部分应该做什么,所以我跳过它:
for (int i = 0; i < 26; i++){
System.out.format("| "+ letters1[i]+" ");
}