gpt4 book ai didi

java - 嵌套循环和数组(频率分析)

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

我想知道是否可以获得一些有关正确增加数组值的帮助。该程序的目的是分析文本文件中各个字母的频率,并将它们记录在一个数组中。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class FrequencyAnaylsis
{
public static String[] alphabet = {"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"};
public static int[] alphabetFrequency = new int[26];
public static int[] alphabetPercentage = new int[26];

FrequencyAnaylsis()
{
}

public static void getFileAndFrequency() throws FileNotFoundException
{
File plaintext = new File("subplaintext");
Scanner inFile = new Scanner(plaintext);

for (int i = 0; i < 26; i++) //specifies the index of alphabet (the letter the program is looking for)
{
while (inFile.hasNext()) //is true when the document has another word following the previous
{
String[] lettersToCompare = inFile.next().toLowerCase().split("(?!^)"); //splits the specified word into a String array

for (int stringIndex = 0; stringIndex < lettersToCompare.length; stringIndex++) //loops through the index (individual letters) of the split word
{
if (lettersToCompare[stringIndex].equals(alphabet[i])) //if letter specified in split word equals letter specified in alphabet
{
alphabetFrequency[i]++; //add one to the frequency array in the same index as the index in alphabet
}
}
}
}
}

public static void getPercentage()
{
int alphabetFrequencyTotal = 0;

for (int i = 0; i < 26; i++)
{
alphabetFrequencyTotal =+ alphabetFrequency[i];
}

for (int i = 0; i < 26; i++)
{
alphabetPercentage[i] = alphabetFrequency[i]/alphabetFrequencyTotal;
}
}

public static void printData()
{
for (int i = 0; i < 26; i++)
{
System.out.println(alphabetFrequency[i]);
}
}

public static void main(String[] args) throws FileNotFoundException
{
FrequencyAnaylsis.getFileAndFrequency();
//FrequencyAnaylsis.getPercentage();
FrequencyAnaylsis.printData();

}
}

当节目读到这句话时:“五年前,一位伟大的美国人,我们站在他的象征性阴影下,签署了《解放黑奴声明》。”,它输出以下内容:

12
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

它能够正确计算“a”的字符数,但不能正确计算任何其他字母的字符数。这是为什么?任何帮助将不胜感激。

最佳答案

您的问题是您尝试使用以下内容对英文字母表中的每个字母浏览一次文件:

public static void getFileAndFrequency() throws FileNotFoundException
{
File plaintext = new File("subplaintext");
Scanner inFile = new Scanner(plaintext);

for (int i = 0; i < 26; i++) //specifies the index of alphabet (the letter the program is looking for)
{
while (inFile.hasNext()) //is true when the document has another word following the previous
{
String[] lettersToCompare = inFile.next().toLowerCase().split("(?!^)"); //splits the specified word into a String array

for (int stringIndex = 0; stringIndex < lettersToCompare.length; stringIndex++) //loops through the index (individual letters) of the split word
{
if (lettersToCompare[stringIndex].equals(alphabet[i])) //if letter specified in split word equals letter specified in alphabet
{
alphabetFrequency[i]++; //add one to the frequency array in the same index as the index in alphabet
}
}
}
}
}

外部循环 ( for (int i = 0; i < 26; i++) ) 运行一次后,您将到达文件末尾,因此所有后续运行都将表现为文件为空。

一个简单的修复方法是更改​​循环的顺序:

public static void getFileAndFrequency() throws FileNotFoundException
{
File plaintext = new File("subplaintext");
Scanner inFile = new Scanner(plaintext);

while (inFile.hasNext()) //is true when the document has another word following the previous
{
String[] lettersToCompare = inFile.next().toLowerCase().split("(?!^)"); //splits the specified word into a String array

for (int i = 0; i < 26; i++) //specifies the index of alphabet (the letter the program is looking for)
{
for (int stringIndex = 0; stringIndex < lettersToCompare.length; stringIndex++) //loops through the index (individual letters) of the split word
{
if (lettersToCompare[stringIndex].equals(alphabet[i])) //if letter specified in split word equals letter specified in alphabet
{
alphabetFrequency[i]++; //add one to the frequency array in the same index as the index in alphabet
}
}
}
}
}

但是,您在内循环中做得太多了。正如@Jägermeister 指出的,你最好使用 Map (例如 HashMap )或利用它,您可以简单地在 alphabetFrequency 中分配索引直接数组:

public static void getFileAndFrequency() throws FileNotFoundException
{
File plaintext = new File("subplaintext");
Scanner inFile = new Scanner(plaintext);

while (inFile.hasNext()) //is true when the document has another word following the previous
{
String[] lettersToCompare = inFile.next().toLowerCase().split("(?!^)"); //splits the specified word into a String array

for (int stringIndex = 0; stringIndex < lettersToCompare.length; stringIndex++) //loops through the index (individual letters) of the split word
{
char ch = lettersToCompare[stringIndex].charAt(0);
if (ch >= 'a' && ch <= 'z')
alphabetFrequency[ch-'a']++; //add one to the frequency array in the same index as the index in alphabet
}
}
}

使用 Map 的示例:

public static Map<Char,Integer> getFileAndFrequency() throws FileNotFoundException
{
Map<Char,Integer> frequencyMap = new HashMap<Char,Integer>();
File plaintext = new File("subplaintext");
Scanner inFile = new Scanner(plaintext);

while (inFile.hasNext()) //is true when the document has another word following the previous
{
String[] lettersToCompare = inFile.next().toLowerCase().split("(?!^)"); //splits the specified word into a String array

for (int stringIndex = 0; stringIndex < lettersToCompare.length; stringIndex++) //loops through the index (individual letters) of the split word
{
char ch = lettersToCompare[stringIndex].charAt(0);
Integer frequency = frequencyMap.get(ch);
if (frequency ==null) {
frequency = 0;
}
frequency += 1;
frequencyMap.put(ch, frequency);
}
}
return frequencyMap;
}

关于java - 嵌套循环和数组(频率分析),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36048620/

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