簡體   English   中英

嵌套循環和數組(頻率分析)

[英]Nested Loops and Arrays (Frequency Analysis)

我想知道是否可以通過正確地將值遞增到數組來獲得一些幫助。 該程序的目的是分析文本文件中單個字母的頻率,並將它們記錄在一個數組中。

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM