Java,文本中n个单词的数量

Java,文本中n个单词的数量,java,string,word,Java,String,Word,此类的目的是统计用户输入的文本中每个n字符单词的出现次数。我认为在方法occurrenceNumber中有一些错误,因为对于每个n字符的单词,我得到228。错误在哪里 import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class TempTest { public static void

此类的目的是统计用户输入的文本中每个n字符单词的出现次数。我认为在方法occurrenceNumber中有一些错误,因为对于每个n字符的单词,我得到228。错误在哪里

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class TempTest {
    public static void main(String[] args) {

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter the integer n");
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        char[] array = { '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', ' ' };
        StringBuffer[] table = createTotalTable(n, array);

        System.out.println("Enter the text ");
        StringBuffer text = new StringBuffer("");
        try {
            text = new StringBuffer(in.readLine());
        } catch (IOException a) {
            System.out.println("Input-Output problem");
        }
        StringBuffer text_formatted = format(text.toString());
        System.out.println("The formatted text is \n" + text_formatted.toString() + "\n");

        System.out.println("Now we print all the n-character words in alphabetic order. Press enter to proceed. ");
        try {
            in.readLine();
        } catch (IOException b) {
            System.out.println("Input-Output problem");
        }
        for (StringBuffer word : table)
            System.out.println(word.toString());

        int[] occurrenceTable = createList(text_formatted, table, n);

        System.out.println("Now we print all the n-words contained in the text with the number of occurrences. Press enter to proceed.");
        try {
            in.readLine();
        } catch (IOException c) {
            System.out.println("Input-Output problem");
        }

        for (int u = 0; u < pow(27, n); u++)
            System.out.println(table[u].toString() + ", " + occurrenceTable[u]);

    }

    public static StringBuffer[] createTotalTable(int n, char[] a) { // this method create an array containing all the n-words in alphabetic order

        StringBuffer[] table = new StringBuffer[pow(27, n)];
        for (int w = 0; w < pow(27, n); w++)
            table[w] = new StringBuffer("");

        for (int h = 1; h <= n; h++) {
            for (int u = 0; u < pow(27, h - 1); u++) {

                for (int j = 0; j < 27; j++) {

                    for (int x = pow(27, n - h + 1) * u + pow(27, n - h) * j; x < pow(27, n - h + 1) * u + pow(27, n - h) * (j + 1); x++)
                        table[x] = table[x].append(a[j]);
                }

            }

        }

        return table;
    }

    public static int pow(int a, int b) { // the method Math.pow modified

        int tot = 1;
        for (int i = 0; i < b; i++)
            tot = a * tot;

        return tot;
    }

    public static int occurrenceNumber(StringBuffer testo, StringBuffer parola, int n) { // this method is aimed to calculate the number of occurrences of a
                                                                                            // word of length n in a text
        int tot = 0;

        if (n > testo.length())
            System.out.println("The integer is bigger than the text's length ");
        else {
            for (int i = 0; i <= testo.length() - n; i++) {
                if (testo.substring(i, i + n) == parola.toString())
                    tot += 1;

            }

        }

        return tot;
    }

    public static int[] createList(StringBuffer str, StringBuffer[] tabella, int n) { // this method is aimed to create an array containing for every position
                                                                                        // the number of occurrences of the corresponding word in the text

        int[] occurrenceTable = new int[pow(27, n)];

        for (int i = 0; i < pow(27, n); i++)
            occurrenceTable[i] = occurrenceNumber(str, tabella[i], n);

        return occurrenceTable;

    }

    public static StringBuffer format(String s) { // this method is aimed to
        // eliminate from the text all non-alphabetic characters and multiple spaces

        s = s.toLowerCase();
        StringBuffer b = new StringBuffer();
        int m = s.length();
        int conta_spazi = 0;
        StringBuffer h = new StringBuffer(s);
        for (int i = 0; i < m; i++) {
            switch (h.charAt(i)) {
            case 'a':
                break;

            case 'A':
                break;

            case 'b':
                break;

            case 'B':
                break;

            case 'c':
                break;

            case 'C':
                break;

            case 'd':
                break;

            case 'D':
                break;

            case 'e':
                break;

            case 'E':
                break;

            case 'f':
                break;

            case 'F':
                break;

            case 'g':
                break;

            case 'G':
                break;

            case 'h':
                break;

            case 'H':
                break;

            case 'i':
                break;

            case 'I':
                break;

            case 'j':
                break;

            case 'J':
                break;

            case 'k':
                break;

            case 'K':
                break;

            case 'l':
                break;

            case 'L':
                break;

            case 'm':
                break;

            case 'M':
                break;

            case 'n':
                break;

            case 'N':
                break;

            case 'o':
                break;

            case 'O':
                break;

            case 'p':
                break;

            case 'P':
                break;

            case 'q':
                break;

            case 'Q':
                break;

            case 'r':
                break;

            case 'R':
                break;

            case 's':
                break;

            case 'S':
                break;

            case 't':
                break;

            case 'T':
                break;

            case 'u':
                break;

            case 'U':
                break;

            case 'v':
                break;

            case 'V':
                break;

            case 'w':
                break;

            case 'W':
                break;

            case 'x':
                break;

            case 'X':
                break;

            case 'y':
                break;

            case 'Y':
                break;

            case 'z':
                break;

            case 'Z':
                break;

            default:
                h.setCharAt(i, ' ');

            }
        }
        for (int i = 0; i < m; i++) {

            if (h.charAt(i) == ' ')
                conta_spazi++;
            else
                conta_spazi = 0;

            if (conta_spazi <= 1)
                b = b.append(h.charAt(i));

        }

        return b;

    }
}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.Scanner;
公共阶级诱惑{
公共静态void main(字符串[]args){
BufferedReader in=新的BufferedReader(新的InputStreamReader(System.in));
System.out.println(“输入整数n”);
扫描仪sc=新的扫描仪(System.in);
int n=sc.nextInt();
char[]数组={'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',''};
StringBuffer[]表=CreateTottalTable(n,数组);
System.out.println(“输入文本”);
StringBuffer文本=新的StringBuffer(“”);
试一试{
text=新的StringBuffer(in.readLine());
}捕获(IOA异常){
System.out.println(“输入输出问题”);
}
StringBuffer text_formatted=格式(text.toString());
System.out.println(“格式化文本为\n”+文本\u格式化.toString()+”\n”);
System.out.println(“现在我们按字母顺序打印所有n个字符的单词。按enter键继续。”);
试一试{
in.readLine();
}捕获(IOB异常){
System.out.println(“输入输出问题”);
}
for(StringBuffer字:表)
System.out.println(word.toString());
int[]occurrenceTable=createList(文本格式,表格,n);
System.out.println(“现在我们用出现的次数打印文本中包含的所有n字。按enter键继续”);
试一试{
in.readLine();
}捕获(IOC异常){
System.out.println(“输入输出问题”);
}
对于(intu=0;u对于(int i=0;i要比较两个字符串,需要调用
equals
方法,而不是
=

您应该替换:

 if(testo.substring(i,i+n) == parola.toString())

if(testo.substring(i,i+n).equals(parola.toString()))

我无法理解您正在实现的逻辑。但是,您可以通过更简单的方法找到计数:

    // extract n-char words and add them to a list
    ArrayList<String> arr = new ArrayList<String>();
    Pattern pattern = Pattern.compile("(^|\\s)\\w{" + n + "}(\\s|$)");
    Matcher matcher = pattern.matcher(text_formatted);
    while (matcher.find()) {
        int k = 0;
        arr.add(matcher.group(k++).trim());
    }
    // sort the list 
    Collections.sort(arr);
    // find the count and print
    String prev = "";
    int count = 1;
    for (int i = 0; i < arr.size(); i++) {
        if (prev.equals(arr.get(i))) {
            count++;
        } else {
            count = 1;
            System.out.print(arr.get(i)+" : ");
        }
        if(i<arr.size()-1 && !arr.get(i).equals(arr.get(i+1)))
        {
            System.out.println(count);
        }
        prev = arr.get(i);
    }
    System.out.println(count);
//提取n字符并将其添加到列表中
ArrayList arr=新的ArrayList();
Pattern=Pattern.compile(“(^ |\\s)\\w{”+n+“}(\\s |$)”;
Matcher Matcher=pattern.Matcher(文本格式);
while(matcher.find()){
int k=0;
arr.add(matcher.group(k++).trim());
}
//对列表排序
集合。排序(arr);
//找到计数并打印
字符串prev=“”;
整数计数=1;
对于(int i=0;i如果(2)按照你的建议,但是现在我得到了每一个n字,结果228 i回答了你的问题。我认为在方法出现错误时有一些错误,因为我为每个n个字符的单词获得了0。错误在哪里?现在如果你没有得到好的结果,那么你应该考虑复习算法和调试你的代码或询问。一个新问题。不要问新问题。修改这个问题-你所问的没有足够的区别。好的,但是mabbas回答了前面的问题,所以我接受了你的建议answer@boris:请注意,这是您提出的与此相关的第四个问题。这次让我们试着让它起作用,而不是在和aski上添加零碎的东西ng新问题!!请注意,此处正在进行的处理太多。例如,由于内存不足,输入n为6或以上将立即使我的IDE上的系统崩溃。我建议您离开并调试程序,然后回来用可调试的更短代码编辑此问题。这可能需要很长时间是时候调试了——你只需要提供必要的代码就可以帮助论坛上的人!