java检查字母的频率

时间:2014-03-26 23:01:03

标签: java arrays

我正在编写一个程序来计算句子的频率,单词数和单词长度。我编写了代码,但在初始化arrays时出错,但我不知道如何使用多种方法。我在x[i]收到错误。

import java.util.Scanner;


public class ScanningText {

    public static int countLetters (String x) {
        int totalLetter, i;
        totalLetter = 0;

        for (i = 0; i < x.length(); i++) {
            for (int upper = 65; upper <= 90; upper++) {
                if (x[i] == upper)
                    totalLetter += 1;
            } for (int lower = 97; lower <= 122; lower++) {
                if (x[i] == lower)
                    totalLetter+=1;
            }
        } return totalLetter;
    }
    public static int words (String x) {
        int totalWord, i;

        for (i = 0; i < x.length(); i++) {
            if (x[i] == 32)
                totalWord += 1;
        } return totalWord;
    }
    public static int longest (String x) {
        int longestWord, wordLength, i;
        longestWord = 0;
        wordLength = 0;

        for (i = 0; i < x.length(); i++) {
            for (int upper = 65; upper <= 90; upper++) {
                if (x[i] == upper)
                    wordLength += 1;
            } for (int lower = 97; lower <= 122; lower++) {
                if (x[i] == lower)
                    wordLength += 1;
            } if (x[i] == 32 || x[i + 1] == 0) {
                if (wordLength > longestWord)
                    longestWord = wordLength;
            wordLength = 0;
            }
        } return longestWord;
    }
    public static int frequency (String x) {
        int letter, i;
        int[] amount;
        amount = new int[26];

        for (i = 0; i < x.length(); i++) {
            for (int upper = 65; upper <= 90; upper++) {
                if (x[i] == upper) {
                    letter = upper;
                    letter = letter - 65;
                    for (int d = 0; d < 26; d++) {
                    // d is occurence of each letter
                        if (d == letter)
                            amount[d] += 1;
                    }
                }
            } for (int lower = 97; lower < 123; lower++) {
                if (x[i] == lower) {
                    letter == lower;
                    letter = letter - 97;
                    for (int k = 0; k <= 25; k++) {
                        if (k == letter)
                            amount[k] += 1;
                    }
                }
            }
        }
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String text;
        int[] amount;
        amount = new int[26];
        String list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        System.out.println("Enter a single line of text");
        text = input.nextLine();

        System.out.println("Total letters: " + countLetters(text));
        System.out.println("Total Words: " + words(text));
        System.out.println("Longest word length is: " + longest(text));
        System.out.println("Frequency is: " + frequency(text));

        for ( int i = 0; i < 26; i++) {
            if (amount[i] != 0)
                System.out.println(list[i] + " -- " + amount[i]);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

x String引用不是数组,

public static int countLetters (String x) {

如果你想在地点找到角色,我会看到charAt()