-7

My code needs to get user input, and print the letter that occurred the most in that input. I know I need to do something along the lines of switching the String input into char[], but what to do after words? how to check for each character's apperance? any ideas how?

I need to do it using only loops & Wrapper classes, it's a part of an assignment so I have to be specific.

  public static void main(String[] args){
    int frequencyCount = 0; //count each letters frequency
    char popularChar = ' '; //letter with the most frequency
    for (int i = 0; i < array.length; i++){
      for(int c = 0; c < array.length; c++){
        frequencyCount++;
      }
}
  • 1
    Have you tried anything yet? (such as counting how often each character appears?) – UnholySheep Oct 13 '16 at 17:19
  • 2
    There are other SO posts that cover this: [http://stackoverflow.com/questions/275944/java-how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string](http://stackoverflow.com/questions/275944/java-how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string) – Iofacture Oct 13 '16 at 17:20
  • You do it very carefully, with a java compiler and, I would recommend, an IDE. – Susannah Potts Oct 13 '16 at 17:23
  • By the way, the code you added does not count as an "attempt", as it doesn't even try to solve the problem and it won't compile (due to the usage of undeclared variables and missing braces) – UnholySheep Oct 13 '16 at 17:56
  • that's not the entire code, I have to do many other thing that I didn't have a problem with, that's y I didn't include the entire code – Khaled Boustati Oct 13 '16 at 18:18

1 Answers1

-2

To get the most frequent character you need to know the frequency of every distinct character in first place.

int frequencyCount = 0, count;
char popularChar, tempChar;
for (int i = 0; i < array.length; i++){
  temp = array[i];
  for(int c = 0; c < array.length; c++){
    count = 0;
    if(tempChar == array[c]){
      count++;}
  }
  if(count > frequencyCount){
      frequencyCount = count;
      popularChar = tempChar;}
}

Hope this will help. And yes, you need to optimize it a lot. Happy coding :)