質問

The method in question is below. This method is supposed to take a string and return the frequency of an item using bag adt. I have it working, however if I (for example) enter "test", it will show the outputs T, S, and T with their respective(and albeit, correct) frequencies. However, I'd like the output to just be T, S with their frequencies.

    public int getFrequency(String str){
        int index=0;
        char[] nArray = new char[sArray.length];

        for(int i=0;i<sArray.length; i++){
            char a = sArray[i];
            String s = Character.toString(a);
            index = consonants.getFrequencyOf(s);
            if(index != 0 && consonants.contains(s)==true){
                for(int x=0;x<nArray.length;x++){
                    if(nArray[i] == sArray[x]){
                        continue;
                    }
                    else{
                        System.out.print(s + ": ");
                        System.out.println(index);
                        nArray[i] = sArray[i];
                        break;
                    }       
                }
            }
        }
        return index;
    }
役に立ちましたか?

解決

You should rather use a HashMap, to store the Characters with their correspondin frequency: -

public Map<Character, Integer> getFrequency(String str){

    String vowels = "aeiouAEIOU";
    Map<Character, Integer> freqMap = new HashMap<Character, Integer>();

    for(int i=0;i<str.length(); i++) {

        char ch = str.charAt(i);

        // If character is a not consonant.. continue with next iteration

        if (vowels.contains(ch)) {
            continue;
        }

        Integer val = freqMap.get(ch);

        if (val != null) {
            // Put new entry in Map.. With character and count = 1

        } else {
            // Increment val by 1, and update the map for this character
        }

    }
    return freqMap;
}

Just a confirmation about your code: -

index = consonants.getFrequencyOf(s);
if(index != 0 && consonants.contains(s)==true){

The 2nd condition looks Vague to me.. Because if index !=0, then it means that consonants contains s.. Then why check again??

Or, your getFrequencyOf(s) is doing something else than checking the containment, that we can't see??

他のヒント

I think the problem is in the else-block. You will get it in any case. But you need to get it only when the cycle ends. You should rewrite internal cycle like this:

                public int getFrequency(String str){
                    char[] sArray = str.toCharArray();
                    char[] nArray = new char[sArray.length];
                    int[] fArray = new int[sArray.length];

                    for(int i=0; i < sArray.length; i++){
                        char a = sArray[i];
                        String s = Character.toString(a);

                        if(consonants.contains(s)==true){        

                            for(int x=0; x <= i; x++){
                                if(nArray[x] == sArray[i]){
                                    fArray[x]++;

                                    break;
                                }

                                if (x == i){
                                    nArray[i] = sArray[i];
                                    fArray[i] = 1;
                                }       
                            }                
                        }
                    }        

                    for (int k = 0; k < fArray.length; ++k){
                       if (nArray[k] == 0){
                           continue;
                       }

                       System.out.println(String.valueOf(sArray[k]) + ": " + String.valueOf(fArray[k]));
                    }

                    return fArray[fArray.length - 1];
                }
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top