1

The purpose of this class is to count the number of occurrences of every n-character words in a text entered by the user. I think there are some mistakes in the method occurrenceNumber because I obtain 228 for every n-character word. Where is the error ?

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;

    }
}
nicael
  • 18,550
  • 13
  • 57
  • 90
boris
  • 353
  • 1
  • 5
  • 15
  • 2
    Nobody will read that much code, without knowing what exactly to look for. Provide some example input, expected output and actual output, and we might be able to figure out what's going wrong. Also, don't repost it as a new question, use the [**edit**](http://stackoverflow.com/posts/18334438/edit) button below your question. – jlordo Aug 20 '13 at 12:01
  • possible duplicate of [Java, Number of n-words in a text](http://stackoverflow.com/questions/18331572/java-number-of-n-words-in-a-text) – jlordo Aug 20 '13 at 12:06
  • Why same question again?http://stackoverflow.com/questions/18331572/java-number-of-n-words-in-a-text – Ruchira Gayan Ranaweera Aug 20 '13 at 12:06
  • 1
    FYI, instead of that huge `switch` statement, you could use `if (!Character.isLetter(h.charAt(i))) h.setCharAt(i, ' ');` – Michelle Aug 20 '13 at 12:07

2 Answers2

3
            if (testo.substring(i, i + n) == parola.toString())
                tot += 1;

Don't use the == operator to compare strings. Use the equals method.

            if (testo.substring(i, i + n).equals(parola.toString()))
                tot += 1;

See for example Java String.equals versus == or How do I compare strings in Java?

Community
  • 1
  • 1
Joni
  • 108,737
  • 14
  • 143
  • 193
2

the code is lone and not very clear, but if you have a string containing the whole thing you can do:

string str = GetTheWholeString();// get the string
int n = 5;// we are searching for 5-lettersWords
String[] words = str.split(' ');
Hashtable wordsAccur = new Hashtable();

for(String currWord : words)
{
   if(currWord.length() == n)
   {
       if(wordsAccur.containsKey(currWord))
       {
          wordsAccur.put(currWord, (int)wordsAccur.get(currWord) + 1);
       }
       else
       {
          wordsAccur.put(currWord, 1);
       }
   }
}

and you can print them, or do what you need with them using:

  Enumeration names;
  names = balance.keys();

  while(names.hasMoreElements()) {
     string curr = (String) names.nextElement();
     System.out.println(curr + ": " +
     wordsAccur.get(curr));
  }
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70