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: –Just a confirmation about your code: –
The 2nd condition looks Vague to me.. Because if
index !=0, then it means thatconsonantscontainss.. Then why check again??Or, your
getFrequencyOf(s)is doing something else than checking the containment, that we can’t see??