The Artima Developer Community
Sponsored Link

Legacy Java Answers Forum
December 2000

Advertisement

Advertisement

This page contains an archived post to the Java Answers Forum made prior to February 25, 2002. If you wish to participate in discussions, please visit the new Artima Forums.

Message:

Help with java problem!

Posted by Tony Cola on December 04, 2000 at 11:34 AM

I have been working through my assignment and have got to the stage where it asks me to find the most frequently occuring character in string. I'M STUCK AND ABOUT TO GO INSANE!!
Here is my code:-
Class Assignment{
String content[] ={
"Hello How Are You AMOLK",
};
int []letterCount =new int[26];
for (int count = 0; count< content.length; count++) {
String current = phrase[count];
char[] letters = current.toCharArray();
for (int count2 = 0; count2char lett= letters[count2];
if ( lett >= 'A') & (lett <= 'Z') ) {
letterCount[lett - 'A']++;
}
}
}
for (char count = 'A'; count <= 'Z'; count++) {
System.out.print(count + ":" +
letterCount[count - 'A'] +
"\t");
}
System.out.println();
}
}
This code produces a list of the number of instances of upper case by each letter. eg. A:1 B:2 C:9 all the way to Z.
However need to work out how to get it to recognise the character that has the highest value. eg in the above list C has the highest frequency.
Thanks for any help!!!

Also any advice on finding the most frequently occuring character from the set ?@$�!. I cannot use if a-z int 29 etc.
Thanks for any help.



Replies:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us