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]
# 1
How would you do it with paper and pencil?Write down a step by step procedure.Translate that into Java.
DrLaszloJamfa at 2007-7-12 0:52:11 > top of Java-index,Java Essentials,Java Programming...
# 2
i guess i have to compared every single character in the string....but what about if it's a 1 million character string?
jra_1574a at 2007-7-12 0:52:11 > top of Java-index,Java Essentials,Java Programming...
# 3
> 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.
DrLaszloJamfa at 2007-7-12 0:52:11 > top of Java-index,Java Essentials,Java Programming...
# 4
> 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.
DrLaszloJamfa at 2007-7-12 0:52:11 > top of Java-index,Java Essentials,Java Programming...
# 5

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;

}

jra_1574a at 2007-7-12 0:52:11 > top of Java-index,Java Essentials,Java Programming...
# 6
That code just counts how many character there are in a string...lleters, digit, spaces, whitespace...
jra_1574a at 2007-7-12 0:52:11 > top of Java-index,Java Essentials,Java Programming...
# 7
How would you do it with paper and pencil?Write down a step by step procedure.Translate that into Java.
DrLaszloJamfa at 2007-7-12 0:52:11 > top of Java-index,Java Essentials,Java Programming...
# 8
Would you like a hint?
DrLaszloJamfa at 2007-7-12 0:52:11 > top of Java-index,Java Essentials,Java Programming...
# 9

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.

maple_shafta at 2007-7-12 0:52:11 > top of Java-index,Java Essentials,Java Programming...
# 10
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"
DrLaszloJamfa at 2007-7-12 0:52:12 > top of Java-index,Java Essentials,Java Programming...
# 11

> 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.

pm_kirkhama at 2007-7-12 0:52:12 > top of Java-index,Java Essentials,Java Programming...
# 12

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.

jra_1574a at 2007-7-12 0:52:12 > top of Java-index,Java Essentials,Java Programming...
# 13
i tried with the StringBuffer class...but something is wrongI am stuck in this method...
jra_1574a at 2007-7-12 0:52:12 > top of Java-index,Java Essentials,Java Programming...
# 14

> 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!

;-)

maple_shafta at 2007-7-12 0:52:12 > top of Java-index,Java Essentials,Java Programming...
# 15
> 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!
DrLaszloJamfa at 2007-7-21 20:11:06 > top of Java-index,Java Essentials,Java Programming...
# 16
> 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!
DrLaszloJamfa at 2007-7-21 20:11:06 > top of Java-index,Java Essentials,Java Programming...