Java 计算URL中字母的出现次数
我试图计算URL中每个字母的出现次数 我发现了这段代码,它似乎起到了作用,但我希望能解释一些事情 1) 我用的是挪威字母表,所以我需要再加三个字母。我将数组更改为29,但它不起作用 2) 你能给我解释一下Java 计算URL中字母的出现次数,java,Java,我试图计算URL中每个字母的出现次数 我发现了这段代码,它似乎起到了作用,但我希望能解释一些事情 1) 我用的是挪威字母表,所以我需要再加三个字母。我将数组更改为29,但它不起作用 2) 你能给我解释一下%c%7d\n是什么意思吗 01 import java.io.FileReader; 02 import java.io.IOException; 03 04 05 public class FrequencyAnalysis { 06 public static
%c%7d\n
是什么意思吗
01 import java.io.FileReader;
02 import java.io.IOException;
03
04
05 public class FrequencyAnalysis {
06 public static void main(String[] args) throws IOException {
07 FileReader reader = new FileReader("PlainTextDocument.txt");
08
09 System.out.println("Letter Frequency");
10
11 int nextChar;
12 char ch;
13
14 // Declare 26 char counting
15 int[] count = new int[26];
16
17 //Loop through the file char
18 while ((nextChar = reader.read()) != -1) {
19 ch = Character.toLowerCase((char) nextChar);
20
21 if (ch >= 'a' && ch <= 'z')
22 count[ch - 'a']++;
23 }
24
25 // Print out
26 for (int i = 0; i < 26; i++) {
27 System.out.printf("%c%7d\n", i + 'A', count[i]);
28 }
29
30 reader.close();
31 }
32 }
01导入java.io.FileReader;
02导入java.io.IOException;
03
04
05公共类频率分析{
06公共静态void main(字符串[]args)引发IOException{
07 FileReader=newfilereader(“PlainTextDocument.txt”);
08
09系统输出打印项次(“字母频率”);
10
11 int nextChar;
12个字符ch;
13
14//26字符计数
15整数[]计数=新整数[26];
16
17//循环浏览文件char
18 while((nextChar=reader.read())!=-1){
19 ch=字符.toLowerCase((char)nextChar);
20
21如果(ch>='a'&&ch您尚未说明如何检查3个额外的字母。增加计数
数组的大小是不够的。您需要在此处说明新字符的unicode点值。这些值可能不再方便地按顺序排列。在这种情况下,您可以使用映射
来存储频率
%c
是unicode字符的格式说明符。%7d
是具有最左侧空格填充的整数说明符。\n
是换行符
记录在中,您没有说明如何检查额外的3个字母。增加计数
数组的大小是不够的。您需要在此处说明新字符的unicode点值。这些值可能不再方便地按顺序排列。在这种情况下,您可以使用映射代码>以存储频率
%c
是unicode字符的格式说明符。%7d
是具有最左侧空格填充的整数说明符。\n
是换行符
记录在中的一件重要事情是,当您希望增加数组中出现的次数时,您将隐式使用字符的ASCII码:
//Here, ch is a char.
ch = Character.toLowerCase((char) nextChar);
//I hate *if statements* without curly brackets but this is off-topic :)
if (ch >= 'a' && ch <= 'z')
/*
* but here, ch is implicitly cast to an integer.
* The int value of a char is its ASCII code.
* for example, the value of 'a' is 97.
* So if ch is 'a', (ch - 'a') = (97 - 97) = 0.
* That's why you are incrementing count[0] in this case.
*
* Now, what happens if ch ='ø'? What is the ASCII code of ø?
* Probably something quite high so that ch-'a' is probably out of bounds
* but the size of your array is 26+3 only.
*
* EDIT : after a quick test, 'ø' = 248.
*
* This would work if norvegian characters had ASCII code between 98 and 100.
*/
count[ch - 'a']++;
//这里,ch是一个字符。
ch=Character.toLowerCase((char)nextChar);
//我讨厌没有花括号的*if语句*,但这是离题的:)
如果(ch>='a'&&ch这里的一个重要问题是,当您想要增加数组中出现的次数时,您隐式地使用字符的ASCII码:
//Here, ch is a char.
ch = Character.toLowerCase((char) nextChar);
//I hate *if statements* without curly brackets but this is off-topic :)
if (ch >= 'a' && ch <= 'z')
/*
* but here, ch is implicitly cast to an integer.
* The int value of a char is its ASCII code.
* for example, the value of 'a' is 97.
* So if ch is 'a', (ch - 'a') = (97 - 97) = 0.
* That's why you are incrementing count[0] in this case.
*
* Now, what happens if ch ='ø'? What is the ASCII code of ø?
* Probably something quite high so that ch-'a' is probably out of bounds
* but the size of your array is 26+3 only.
*
* EDIT : after a quick test, 'ø' = 248.
*
* This would work if norvegian characters had ASCII code between 98 and 100.
*/
count[ch - 'a']++;
//这里,ch是一个字符。
ch=Character.toLowerCase((char)nextChar);
//我讨厌没有花括号的*if语句*,但这是离题的:)
如果(ch>='a'&&ch