I need help
Hello everyone!
I have 1 question...
How do i write a method that accepts a String as argument and returns the most frequent character in the string...This is using Java 5. my compiler is JCreator.....
Can anyone give me some idea.?
or a code that may do it....
thanks!
[306 byte] By [
jra_1574a] at [2007-11-27 1:39:19]

How would you do it with paper and pencil?Write down a step by step procedure.Translate that into Java.
i guess i have to compared every single character in the string....but what about if it's a 1 million character string?
> but what about if it's a 1 million character string?A million characters is nothing. But, if you're trying to thinkahead to optimizations, solve the problem first.
> guess i have to compared every single character in the string....This is vague and far from a step-by-step description of howyou would solve the problem with paper and pencil.Expand on this. Be precise.
public static String getMostFrequent (String str)
{
int characterMatch = 0;
for (int i =0; i < str.length();i++)
{
if (str.charAt(i) == str.charAt(i))
characterMatch++;
}
mostFrequent = String.valueOf(characterMatch);
return mostFrequent;
}
That code just counts how many character there are in a string...lleters, digit, spaces, whitespace...
How would you do it with paper and pencil?Write down a step by step procedure.Translate that into Java.
1) Convert your String to a StringBuffer.
2) Capture the first character in the StringBuffer.
3) Compare that character to every character in the StringBuffer. For every match, increment a counter and remove that character from the StringBuffer.
4) Place the character and counter into a data object and add that data object to a Set or other collection, Set's are better because they are ordered.
5) Loop through steps 2 through 4 until the StringBuffer length = 0.
6) Easily retrieve the character with the highest counter from your ordered Set and return that character value.
Er, my hint was going to be:In which string is it easier to determine the letter that occurs with greatest frequency?"The quick brown fox jumped over the lazy dog""Tabcddeeeefghhijklmnoooopqrrtuuvwxyz"
> i guess i have to compared every single character in the string....
You certainly have to look at each character once to count them. You don't need to compare them to each other or sort them.
Imaging you had to count a flock of up-to five (or 12*) black or white sheep walking past one at a time, and tell me whether there were more black or white ones. Could you do that on your fingers? Would you need to compare each sheep with each other, or sort the sheep into groups of the same colour?
> but what about if it's a 1 million character string?
Then you have to look at a million characters.
* traditionally north European shepherds (and others) counted on their fingers by touching thumb to each finger bone, giving base 12, hence you can count up to 12 dozen on both hands, and the many base 12 things in pre-decimal numbers. It's also a lot faster than counting in your head.
I kind of get the idea of the String Buffer thing....
I just don't understand from step 4 to the last step... I get the loop idea... but not the other steps... I am new with this java language....
4) Place the character and counter into a data object and add that data object to a Set or other collection, Set's are better because they are ordered.
5) Loop through steps 2 through 4 until the StringBuffer length = 0.
6) Easily retrieve the character with the highest counter from your ordered Set and return that character value.
i tried with the StringBuffer class...but something is wrongI am stuck in this method...
> Er, my hint was going to be:
>
> In which string is it easier to determine the letter
> that occurs with greatest frequency?
>
> "The quick brown fox jumped over the lazy dog"
>
> "Tabcddeeeefghhijklmnoooopqrrtuuvwxyz"
>
Oh! That is a much better way of doing this!
;-)
> i tried with the StringBuffer class...but something is wrong> I am stuck in this method...Let's see some code. So far it's just been vague mumblings from you!
> Oh! That is a much better way of doing this!The prof in the algorithm class I took (himself a student of Knuth)gave us a rule of thumb: if you're not sure what to do first, sort!