static int countChars( String str, char searchChar ) {
// Count the number of times searchChar occurs in
// str and return the result.
int i; // A position in the string, str.
char ch; // A character in the string.
int count; // Number of times searchChar has been found in str.
count = 0;
for ( i = 0; i < str.length(); i++ ) {
ch = str.charAt(i); // Get the i-th character in str.
if ( ch == searchChar )
count++;
}
return count;
}

>static int countChars( String str, char searchChar )
This only returns the count for a single character. So to find the most common character, not only would you be forced to call countChars for every character in the string, you would also need to find a character with the largest count. That's a little too much work for my tastes, and it's not very efficient either.

How about a complete program that solves the problem with only eight more logical lines than your single method and is more than twice as fast:

Made it a bit smarter, couldn't help tinker and use some more features of the Tiger :)
The explicit cast to int Narue used was not needed, and the iteration can be performed using the new for-each loop in 4 lines less code than she used ;)

indeed. I'd have changed it to m except that might be seen as slightly pickish :)
Of course using JBuilder it's extremely easy to replace all instances of a name with another, just click on the block edit button and change any one of them to change them all :cheesy:

>I'd have changed it to m except that might be seen as slightly pickish
x is the better choice anyway, or my personal favorite identifier for "a chunk of memory that holds something": shtoobie. My coworkers love it when I give them code with declarations like this:

Here at DaniWeb we try to help people learn Java and develop their Java skills. We do NOT do people's homework for them. Your post explains and teaches nothing. In future please help by pointing people in the right direction - eg tell them which classes and methods they should read about, or give them some sample code that they will have to understand and adapt to their needs. If you feel you should correct someone's code then that's useless if you don't explain what you changed and why you changed it as you did. If you need to explain with actual code then explain why you coded it the way you did. Don't just spoon-feed them a solution to copy and paste.