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

Java 嵌套循环和数组(频率分析),java,arrays,loops,Java,Arrays,Loops,我想知道我是否可以得到一些帮助来正确地增加数组的值。这个程序的目的是分析文本文件中单个字母的频率,并将它们记录在一个数组中 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

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

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();

}
}

在“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
                }
            }
        }   
    }
}
使用
地图的示例

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;
}
public静态映射getFileAndFrequency()引发FileNotFoundException
{
Map frequencyMap=new HashMap();
文件明文=新文件(“子文本”);
扫描仪内嵌=新扫描仪(纯文本);
while(infle.hasNext())//当文档在前一个单词后面有另一个单词时为true
{   
String[]lettersToCompare=infle.next().toLowerCase().split((?!^);//将指定的单词拆分为字符串数组
for(int-stringIndex=0;stringIndex
您的问题是,您试图对英文字母表中的每个字母检查一次文件,如下所示:

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 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;
}
public静态映射getFileAndFrequency()引发FileNotFoundException
{
Map frequencyMap=new HashMap();
文件明文=新文件(“子文本”);
扫描仪内嵌=新扫描仪(纯文本);
while(infle.hasNext())//当文档在前一个单词后面有另一个单词时为true
{   
String[]lettersToCompare=infle.next().toLowerCase().split((?!^);//将指定的单词拆分为字符串数组
for(int-stringIndex=0;stringIndex
建议:阅读hashmaps。如果使用字符作为映射键;然后可以使用简单整数(as)计数器作为映射值。然后您只需迭代输入字符串;在地图中使用每个字符进行查找。或者,作为替代:请记住,
a
可以很容易地映射到整数0<代码>b映射到1。。。等等意思是:你不需要把a-z放在一个数组中;只需从输入字符计算索引位置。建议:阅读hashmaps。如果使用字符作为映射键;然后可以使用简单整数(as)计数器作为映射值。然后您只需迭代输入字符串;在地图中使用每个字符进行查找。或者,作为替代:请记住,
a
可以很容易地映射到整数0<代码>b映射到1。。。等等意思是:你不需要把a-z放在一个数组中;只需从输入字符计算索引位置。
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;
}