質問

I have a problem with a part of my program. In the following code 27 is the number of letters of our alphabet; list_extended is an objcet of type Hashtable(StringBuffer,Integer), containing all strings of length n+1 in the text with the number of occurrences; list_extended is built correctly because I have already controlled this part. The aim is : for every repetition of the outer for we take te last n characters of text generated, and for every letter of the alphabet we count the number of occurrences in list_extended of the n+1 characters obtained by adding the character to the last n characters of text_generated; then we choose the letter with the biggest number of occurrences. The result I obtain is that occurrences contains all 0's, why ? The code

        int index;
        int[] occurrences = new int[27];
        StringBuffer curr_word;
        for(int x = 0; x < m; x++){   // m is the number of characters the user wants to add
            curr_word = new StringBuffer(text_generated.substring(x, x+n)); // n is an integer entered previously, initially text_generated is formed by n characters
            for(int j = 0; j < 27; j++){
                if(list_extended.get(curr_word.append(array[j]))==null)
                    occurrences[j] = 0;
                else
                    occurrences[j] =(int) list_extended.get(curr_word.append(array[j]));
            }
            index = 0;
            for(int j = 1; j < 27; j++){
                if(occurrences[j] > occurrences[index])
                    index = j;
            }   
            text_generated = text_generated.append(array[index]);
        }
                    System.out.println("The text generatd is \n" + text_generated.toString());
役に立ちましたか?

解決

Because you create new object curr_word, but you didn't put it in list_extended, so every time you check

if(list_extended.get(curr_word.append(array[j]))==null)

will be null and

occurrences[j] will be 0

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top