Find the most repeated word in a string [closed]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-2















can you give me some pointers as of how can I find the most frequent word in an String? I cannot use Maps, lists or so on. I should only achieve this by for's and if's and some in-build methods.










share|improve this question













closed as too broad by ernest_k, gofr1, Vogel612, Patrick Mevzek, tk421 Nov 16 '18 at 16:28


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.



















  • 1) Remove punctuation 2) split sentence by spaces 3) map of words -> frequency 4) iterate map, get key with highest value

    – Michael
    Nov 16 '18 at 12:33











  • @Michael ... I cannot use Maps...

    – OldCurmudgeon
    Nov 16 '18 at 12:35











  • Maybe you could sort your array (after splitting the string) and count adjacent words, resetting on word changes, and looking for the max of repeats

    – ernest_k
    Nov 16 '18 at 12:35













  • @Michael I cannot use maps.

    – Uponn
    Nov 16 '18 at 12:35






  • 1





    @Uponn Ah, one of those poorly designed assignments that forces arbitrary constraints on students because they couldn't come up with an exercise that's actually appropriate for the material that they're teaching. Good luck with that!

    – Michael
    Nov 16 '18 at 12:39


















-2















can you give me some pointers as of how can I find the most frequent word in an String? I cannot use Maps, lists or so on. I should only achieve this by for's and if's and some in-build methods.










share|improve this question













closed as too broad by ernest_k, gofr1, Vogel612, Patrick Mevzek, tk421 Nov 16 '18 at 16:28


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.



















  • 1) Remove punctuation 2) split sentence by spaces 3) map of words -> frequency 4) iterate map, get key with highest value

    – Michael
    Nov 16 '18 at 12:33











  • @Michael ... I cannot use Maps...

    – OldCurmudgeon
    Nov 16 '18 at 12:35











  • Maybe you could sort your array (after splitting the string) and count adjacent words, resetting on word changes, and looking for the max of repeats

    – ernest_k
    Nov 16 '18 at 12:35













  • @Michael I cannot use maps.

    – Uponn
    Nov 16 '18 at 12:35






  • 1





    @Uponn Ah, one of those poorly designed assignments that forces arbitrary constraints on students because they couldn't come up with an exercise that's actually appropriate for the material that they're teaching. Good luck with that!

    – Michael
    Nov 16 '18 at 12:39














-2












-2








-2








can you give me some pointers as of how can I find the most frequent word in an String? I cannot use Maps, lists or so on. I should only achieve this by for's and if's and some in-build methods.










share|improve this question














can you give me some pointers as of how can I find the most frequent word in an String? I cannot use Maps, lists or so on. I should only achieve this by for's and if's and some in-build methods.







java






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 12:22









UponnUponn

45




45




closed as too broad by ernest_k, gofr1, Vogel612, Patrick Mevzek, tk421 Nov 16 '18 at 16:28


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as too broad by ernest_k, gofr1, Vogel612, Patrick Mevzek, tk421 Nov 16 '18 at 16:28


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.















  • 1) Remove punctuation 2) split sentence by spaces 3) map of words -> frequency 4) iterate map, get key with highest value

    – Michael
    Nov 16 '18 at 12:33











  • @Michael ... I cannot use Maps...

    – OldCurmudgeon
    Nov 16 '18 at 12:35











  • Maybe you could sort your array (after splitting the string) and count adjacent words, resetting on word changes, and looking for the max of repeats

    – ernest_k
    Nov 16 '18 at 12:35













  • @Michael I cannot use maps.

    – Uponn
    Nov 16 '18 at 12:35






  • 1





    @Uponn Ah, one of those poorly designed assignments that forces arbitrary constraints on students because they couldn't come up with an exercise that's actually appropriate for the material that they're teaching. Good luck with that!

    – Michael
    Nov 16 '18 at 12:39



















  • 1) Remove punctuation 2) split sentence by spaces 3) map of words -> frequency 4) iterate map, get key with highest value

    – Michael
    Nov 16 '18 at 12:33











  • @Michael ... I cannot use Maps...

    – OldCurmudgeon
    Nov 16 '18 at 12:35











  • Maybe you could sort your array (after splitting the string) and count adjacent words, resetting on word changes, and looking for the max of repeats

    – ernest_k
    Nov 16 '18 at 12:35













  • @Michael I cannot use maps.

    – Uponn
    Nov 16 '18 at 12:35






  • 1





    @Uponn Ah, one of those poorly designed assignments that forces arbitrary constraints on students because they couldn't come up with an exercise that's actually appropriate for the material that they're teaching. Good luck with that!

    – Michael
    Nov 16 '18 at 12:39

















1) Remove punctuation 2) split sentence by spaces 3) map of words -> frequency 4) iterate map, get key with highest value

– Michael
Nov 16 '18 at 12:33





1) Remove punctuation 2) split sentence by spaces 3) map of words -> frequency 4) iterate map, get key with highest value

– Michael
Nov 16 '18 at 12:33













@Michael ... I cannot use Maps...

– OldCurmudgeon
Nov 16 '18 at 12:35





@Michael ... I cannot use Maps...

– OldCurmudgeon
Nov 16 '18 at 12:35













Maybe you could sort your array (after splitting the string) and count adjacent words, resetting on word changes, and looking for the max of repeats

– ernest_k
Nov 16 '18 at 12:35







Maybe you could sort your array (after splitting the string) and count adjacent words, resetting on word changes, and looking for the max of repeats

– ernest_k
Nov 16 '18 at 12:35















@Michael I cannot use maps.

– Uponn
Nov 16 '18 at 12:35





@Michael I cannot use maps.

– Uponn
Nov 16 '18 at 12:35




1




1





@Uponn Ah, one of those poorly designed assignments that forces arbitrary constraints on students because they couldn't come up with an exercise that's actually appropriate for the material that they're teaching. Good luck with that!

– Michael
Nov 16 '18 at 12:39





@Uponn Ah, one of those poorly designed assignments that forces arbitrary constraints on students because they couldn't come up with an exercise that's actually appropriate for the material that they're teaching. Good luck with that!

– Michael
Nov 16 '18 at 12:39












4 Answers
4






active

oldest

votes


















1














Split String and save to array, sort the array, iterate over the sorted array and count frequency of same strings updating the maximal count. Example:



public static void main(String args) {
String myStr = "how can I find the most frequent word in an string how can I find how how how string";
String splited = myStr.split(" ");
Arrays.sort(splited);
System.out.println(Arrays.toString(splited));
int max = 0;
int count= 1;
String word = splited[0];
String curr = splited[0];
for(int i = 1; i<splited.length; i++){
if(splited[i].equals(curr)){
count++;
}
else{
count =1;
curr = splited[i];
}
if(max<count){
max = count;
word = splited[i];
}
}
System.out.println(max + " x " + word);
}





share|improve this answer
























  • You can probably move if(max<count){ into the else block.

    – ernest_k
    Nov 16 '18 at 12:55











  • Does not handel equal frequency. If there are strings with equal frequency only the first string is returned.

    – Eritrean
    Nov 16 '18 at 13:01











  • @Uponn How this answer is the best ?? Arrays IE being used and this is case insensitive operation ?? I though you said not to use Array or related classes ?

    – Hades
    Nov 17 '18 at 11:27



















0














Sample idea (there are thousand ways to solve this):



1: A B B C B (< String with words, seperated by blanks)
'A' is your start position



2: count the A (1) and save the pos of A (0). You always iterate from pos until the end of the String.



3: continue counting until you iterated over the entire String. When you reached the end of the String save the count by assigning it to another variable (e.g. oldCount).



4: move on to the next word and start counting B's (new position = 1). You are about to count 3 B's. If newer count > older count replace the older count.



5: count the next word and update the position to your current position, which is 3. (which is the last position of the String).



6: you are not gonna update the counter, B is the most used word in the String.






share|improve this answer































    0














    For the purists - just loops and String.



    private String mostFrequentWord(String words) {
    // Where my current word starts.
    int wordStart = 0;
    // How many I counted.
    int wordCount = 0;
    // The currently most frequent.
    String word = "";
    for (int wordEnd = wordStart; wordEnd < words.length(); wordEnd++) {
    // Is this the end of a word?
    if (wordEnd > words.length() || words.charAt(wordEnd) == ' ') {
    // We have a word! How many times does it occur?
    String thisWord = words.substring(wordStart, wordEnd);
    // How many times this word occurs.
    int thisWordCount = 0;
    // Current start of search.
    int search = -1;
    // Count them.
    while ((search = words.indexOf(thisWord, search + 1)) >= 0) {
    thisWordCount += 1;
    }
    // Is it longer?
    if (thisWordCount > wordCount) {
    // Keep track.
    word = thisWord;
    wordCount = thisWordCount;
    }
    // Move start to the next word.
    wordStart = wordEnd + 1;
    }
    }
    return word;
    }

    private void test() {
    String words = "Now is the time for all good men to come to the aid of the party";
    System.out.println("Most frequent word in "" + words + "" is " + mostFrequentWord(words));
    }





    share|improve this answer































      0














      public static void main(String...strings) {
      String para = "Paris in the the spring.Not that that is related.Why are you laughing? Are my my regular expressions THAT bad??";
      String words = para.split("\s+");
      int finalCount = 0;
      int tempCount = 0;
      String mostlyUsedWord = null;
      for (String word: words) {
      tempCount = 0;
      for (String w: words) {
      if (word.equalsIgnoreCase(w)) {
      tempCount++;
      }
      }
      if (tempCount >= finalCount) {
      finalCount = tempCount;
      mostlyUsedWord = word;
      }
      }
      System.out.println("mostlyUsedWord:: = " + mostlyUsedWord + " ,count:: = " + finalCount);
      }





      share|improve this answer


























      • This will gives "that" as the result, the search is case insensitive

        – Hades
        Nov 16 '18 at 12:47











      • I think Pattern is a bit oversized here. Why not use an inner loop and String.equals?

        – Ralf Renz
        Nov 16 '18 at 12:51











      • Thanks, it most likely words but as I said I have to use only for loops, if statements and some inbuild methods (indexOf, compare, isEqual and etc.). We still haven't learned about Pattern and Matcher so I have no idea what they do at the moment.

        – Uponn
        Nov 16 '18 at 12:54











      • @Hades I've picked the other answer as best because we still haven't learned for-each which you've used. The other answer is easy enough for me to understand what is happening also.

        – Uponn
        Nov 17 '18 at 12:33











      • @Uponn but you explicitly mentioned not to use List , I havent , anyways whatever works for you.

        – Hades
        Nov 17 '18 at 12:42


















      4 Answers
      4






      active

      oldest

      votes








      4 Answers
      4






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Split String and save to array, sort the array, iterate over the sorted array and count frequency of same strings updating the maximal count. Example:



      public static void main(String args) {
      String myStr = "how can I find the most frequent word in an string how can I find how how how string";
      String splited = myStr.split(" ");
      Arrays.sort(splited);
      System.out.println(Arrays.toString(splited));
      int max = 0;
      int count= 1;
      String word = splited[0];
      String curr = splited[0];
      for(int i = 1; i<splited.length; i++){
      if(splited[i].equals(curr)){
      count++;
      }
      else{
      count =1;
      curr = splited[i];
      }
      if(max<count){
      max = count;
      word = splited[i];
      }
      }
      System.out.println(max + " x " + word);
      }





      share|improve this answer
























      • You can probably move if(max<count){ into the else block.

        – ernest_k
        Nov 16 '18 at 12:55











      • Does not handel equal frequency. If there are strings with equal frequency only the first string is returned.

        – Eritrean
        Nov 16 '18 at 13:01











      • @Uponn How this answer is the best ?? Arrays IE being used and this is case insensitive operation ?? I though you said not to use Array or related classes ?

        – Hades
        Nov 17 '18 at 11:27
















      1














      Split String and save to array, sort the array, iterate over the sorted array and count frequency of same strings updating the maximal count. Example:



      public static void main(String args) {
      String myStr = "how can I find the most frequent word in an string how can I find how how how string";
      String splited = myStr.split(" ");
      Arrays.sort(splited);
      System.out.println(Arrays.toString(splited));
      int max = 0;
      int count= 1;
      String word = splited[0];
      String curr = splited[0];
      for(int i = 1; i<splited.length; i++){
      if(splited[i].equals(curr)){
      count++;
      }
      else{
      count =1;
      curr = splited[i];
      }
      if(max<count){
      max = count;
      word = splited[i];
      }
      }
      System.out.println(max + " x " + word);
      }





      share|improve this answer
























      • You can probably move if(max<count){ into the else block.

        – ernest_k
        Nov 16 '18 at 12:55











      • Does not handel equal frequency. If there are strings with equal frequency only the first string is returned.

        – Eritrean
        Nov 16 '18 at 13:01











      • @Uponn How this answer is the best ?? Arrays IE being used and this is case insensitive operation ?? I though you said not to use Array or related classes ?

        – Hades
        Nov 17 '18 at 11:27














      1












      1








      1







      Split String and save to array, sort the array, iterate over the sorted array and count frequency of same strings updating the maximal count. Example:



      public static void main(String args) {
      String myStr = "how can I find the most frequent word in an string how can I find how how how string";
      String splited = myStr.split(" ");
      Arrays.sort(splited);
      System.out.println(Arrays.toString(splited));
      int max = 0;
      int count= 1;
      String word = splited[0];
      String curr = splited[0];
      for(int i = 1; i<splited.length; i++){
      if(splited[i].equals(curr)){
      count++;
      }
      else{
      count =1;
      curr = splited[i];
      }
      if(max<count){
      max = count;
      word = splited[i];
      }
      }
      System.out.println(max + " x " + word);
      }





      share|improve this answer













      Split String and save to array, sort the array, iterate over the sorted array and count frequency of same strings updating the maximal count. Example:



      public static void main(String args) {
      String myStr = "how can I find the most frequent word in an string how can I find how how how string";
      String splited = myStr.split(" ");
      Arrays.sort(splited);
      System.out.println(Arrays.toString(splited));
      int max = 0;
      int count= 1;
      String word = splited[0];
      String curr = splited[0];
      for(int i = 1; i<splited.length; i++){
      if(splited[i].equals(curr)){
      count++;
      }
      else{
      count =1;
      curr = splited[i];
      }
      if(max<count){
      max = count;
      word = splited[i];
      }
      }
      System.out.println(max + " x " + word);
      }






      share|improve this answer












      share|improve this answer



      share|improve this answer










      answered Nov 16 '18 at 12:53









      EritreanEritrean

      3,92311015




      3,92311015













      • You can probably move if(max<count){ into the else block.

        – ernest_k
        Nov 16 '18 at 12:55











      • Does not handel equal frequency. If there are strings with equal frequency only the first string is returned.

        – Eritrean
        Nov 16 '18 at 13:01











      • @Uponn How this answer is the best ?? Arrays IE being used and this is case insensitive operation ?? I though you said not to use Array or related classes ?

        – Hades
        Nov 17 '18 at 11:27



















      • You can probably move if(max<count){ into the else block.

        – ernest_k
        Nov 16 '18 at 12:55











      • Does not handel equal frequency. If there are strings with equal frequency only the first string is returned.

        – Eritrean
        Nov 16 '18 at 13:01











      • @Uponn How this answer is the best ?? Arrays IE being used and this is case insensitive operation ?? I though you said not to use Array or related classes ?

        – Hades
        Nov 17 '18 at 11:27

















      You can probably move if(max<count){ into the else block.

      – ernest_k
      Nov 16 '18 at 12:55





      You can probably move if(max<count){ into the else block.

      – ernest_k
      Nov 16 '18 at 12:55













      Does not handel equal frequency. If there are strings with equal frequency only the first string is returned.

      – Eritrean
      Nov 16 '18 at 13:01





      Does not handel equal frequency. If there are strings with equal frequency only the first string is returned.

      – Eritrean
      Nov 16 '18 at 13:01













      @Uponn How this answer is the best ?? Arrays IE being used and this is case insensitive operation ?? I though you said not to use Array or related classes ?

      – Hades
      Nov 17 '18 at 11:27





      @Uponn How this answer is the best ?? Arrays IE being used and this is case insensitive operation ?? I though you said not to use Array or related classes ?

      – Hades
      Nov 17 '18 at 11:27













      0














      Sample idea (there are thousand ways to solve this):



      1: A B B C B (< String with words, seperated by blanks)
      'A' is your start position



      2: count the A (1) and save the pos of A (0). You always iterate from pos until the end of the String.



      3: continue counting until you iterated over the entire String. When you reached the end of the String save the count by assigning it to another variable (e.g. oldCount).



      4: move on to the next word and start counting B's (new position = 1). You are about to count 3 B's. If newer count > older count replace the older count.



      5: count the next word and update the position to your current position, which is 3. (which is the last position of the String).



      6: you are not gonna update the counter, B is the most used word in the String.






      share|improve this answer




























        0














        Sample idea (there are thousand ways to solve this):



        1: A B B C B (< String with words, seperated by blanks)
        'A' is your start position



        2: count the A (1) and save the pos of A (0). You always iterate from pos until the end of the String.



        3: continue counting until you iterated over the entire String. When you reached the end of the String save the count by assigning it to another variable (e.g. oldCount).



        4: move on to the next word and start counting B's (new position = 1). You are about to count 3 B's. If newer count > older count replace the older count.



        5: count the next word and update the position to your current position, which is 3. (which is the last position of the String).



        6: you are not gonna update the counter, B is the most used word in the String.






        share|improve this answer


























          0












          0








          0







          Sample idea (there are thousand ways to solve this):



          1: A B B C B (< String with words, seperated by blanks)
          'A' is your start position



          2: count the A (1) and save the pos of A (0). You always iterate from pos until the end of the String.



          3: continue counting until you iterated over the entire String. When you reached the end of the String save the count by assigning it to another variable (e.g. oldCount).



          4: move on to the next word and start counting B's (new position = 1). You are about to count 3 B's. If newer count > older count replace the older count.



          5: count the next word and update the position to your current position, which is 3. (which is the last position of the String).



          6: you are not gonna update the counter, B is the most used word in the String.






          share|improve this answer













          Sample idea (there are thousand ways to solve this):



          1: A B B C B (< String with words, seperated by blanks)
          'A' is your start position



          2: count the A (1) and save the pos of A (0). You always iterate from pos until the end of the String.



          3: continue counting until you iterated over the entire String. When you reached the end of the String save the count by assigning it to another variable (e.g. oldCount).



          4: move on to the next word and start counting B's (new position = 1). You are about to count 3 B's. If newer count > older count replace the older count.



          5: count the next word and update the position to your current position, which is 3. (which is the last position of the String).



          6: you are not gonna update the counter, B is the most used word in the String.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 12:47









          elpelp

          369116




          369116























              0














              For the purists - just loops and String.



              private String mostFrequentWord(String words) {
              // Where my current word starts.
              int wordStart = 0;
              // How many I counted.
              int wordCount = 0;
              // The currently most frequent.
              String word = "";
              for (int wordEnd = wordStart; wordEnd < words.length(); wordEnd++) {
              // Is this the end of a word?
              if (wordEnd > words.length() || words.charAt(wordEnd) == ' ') {
              // We have a word! How many times does it occur?
              String thisWord = words.substring(wordStart, wordEnd);
              // How many times this word occurs.
              int thisWordCount = 0;
              // Current start of search.
              int search = -1;
              // Count them.
              while ((search = words.indexOf(thisWord, search + 1)) >= 0) {
              thisWordCount += 1;
              }
              // Is it longer?
              if (thisWordCount > wordCount) {
              // Keep track.
              word = thisWord;
              wordCount = thisWordCount;
              }
              // Move start to the next word.
              wordStart = wordEnd + 1;
              }
              }
              return word;
              }

              private void test() {
              String words = "Now is the time for all good men to come to the aid of the party";
              System.out.println("Most frequent word in "" + words + "" is " + mostFrequentWord(words));
              }





              share|improve this answer




























                0














                For the purists - just loops and String.



                private String mostFrequentWord(String words) {
                // Where my current word starts.
                int wordStart = 0;
                // How many I counted.
                int wordCount = 0;
                // The currently most frequent.
                String word = "";
                for (int wordEnd = wordStart; wordEnd < words.length(); wordEnd++) {
                // Is this the end of a word?
                if (wordEnd > words.length() || words.charAt(wordEnd) == ' ') {
                // We have a word! How many times does it occur?
                String thisWord = words.substring(wordStart, wordEnd);
                // How many times this word occurs.
                int thisWordCount = 0;
                // Current start of search.
                int search = -1;
                // Count them.
                while ((search = words.indexOf(thisWord, search + 1)) >= 0) {
                thisWordCount += 1;
                }
                // Is it longer?
                if (thisWordCount > wordCount) {
                // Keep track.
                word = thisWord;
                wordCount = thisWordCount;
                }
                // Move start to the next word.
                wordStart = wordEnd + 1;
                }
                }
                return word;
                }

                private void test() {
                String words = "Now is the time for all good men to come to the aid of the party";
                System.out.println("Most frequent word in "" + words + "" is " + mostFrequentWord(words));
                }





                share|improve this answer


























                  0












                  0








                  0







                  For the purists - just loops and String.



                  private String mostFrequentWord(String words) {
                  // Where my current word starts.
                  int wordStart = 0;
                  // How many I counted.
                  int wordCount = 0;
                  // The currently most frequent.
                  String word = "";
                  for (int wordEnd = wordStart; wordEnd < words.length(); wordEnd++) {
                  // Is this the end of a word?
                  if (wordEnd > words.length() || words.charAt(wordEnd) == ' ') {
                  // We have a word! How many times does it occur?
                  String thisWord = words.substring(wordStart, wordEnd);
                  // How many times this word occurs.
                  int thisWordCount = 0;
                  // Current start of search.
                  int search = -1;
                  // Count them.
                  while ((search = words.indexOf(thisWord, search + 1)) >= 0) {
                  thisWordCount += 1;
                  }
                  // Is it longer?
                  if (thisWordCount > wordCount) {
                  // Keep track.
                  word = thisWord;
                  wordCount = thisWordCount;
                  }
                  // Move start to the next word.
                  wordStart = wordEnd + 1;
                  }
                  }
                  return word;
                  }

                  private void test() {
                  String words = "Now is the time for all good men to come to the aid of the party";
                  System.out.println("Most frequent word in "" + words + "" is " + mostFrequentWord(words));
                  }





                  share|improve this answer













                  For the purists - just loops and String.



                  private String mostFrequentWord(String words) {
                  // Where my current word starts.
                  int wordStart = 0;
                  // How many I counted.
                  int wordCount = 0;
                  // The currently most frequent.
                  String word = "";
                  for (int wordEnd = wordStart; wordEnd < words.length(); wordEnd++) {
                  // Is this the end of a word?
                  if (wordEnd > words.length() || words.charAt(wordEnd) == ' ') {
                  // We have a word! How many times does it occur?
                  String thisWord = words.substring(wordStart, wordEnd);
                  // How many times this word occurs.
                  int thisWordCount = 0;
                  // Current start of search.
                  int search = -1;
                  // Count them.
                  while ((search = words.indexOf(thisWord, search + 1)) >= 0) {
                  thisWordCount += 1;
                  }
                  // Is it longer?
                  if (thisWordCount > wordCount) {
                  // Keep track.
                  word = thisWord;
                  wordCount = thisWordCount;
                  }
                  // Move start to the next word.
                  wordStart = wordEnd + 1;
                  }
                  }
                  return word;
                  }

                  private void test() {
                  String words = "Now is the time for all good men to come to the aid of the party";
                  System.out.println("Most frequent word in "" + words + "" is " + mostFrequentWord(words));
                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 16 '18 at 12:55









                  OldCurmudgeonOldCurmudgeon

                  52.7k1388172




                  52.7k1388172























                      0














                      public static void main(String...strings) {
                      String para = "Paris in the the spring.Not that that is related.Why are you laughing? Are my my regular expressions THAT bad??";
                      String words = para.split("\s+");
                      int finalCount = 0;
                      int tempCount = 0;
                      String mostlyUsedWord = null;
                      for (String word: words) {
                      tempCount = 0;
                      for (String w: words) {
                      if (word.equalsIgnoreCase(w)) {
                      tempCount++;
                      }
                      }
                      if (tempCount >= finalCount) {
                      finalCount = tempCount;
                      mostlyUsedWord = word;
                      }
                      }
                      System.out.println("mostlyUsedWord:: = " + mostlyUsedWord + " ,count:: = " + finalCount);
                      }





                      share|improve this answer


























                      • This will gives "that" as the result, the search is case insensitive

                        – Hades
                        Nov 16 '18 at 12:47











                      • I think Pattern is a bit oversized here. Why not use an inner loop and String.equals?

                        – Ralf Renz
                        Nov 16 '18 at 12:51











                      • Thanks, it most likely words but as I said I have to use only for loops, if statements and some inbuild methods (indexOf, compare, isEqual and etc.). We still haven't learned about Pattern and Matcher so I have no idea what they do at the moment.

                        – Uponn
                        Nov 16 '18 at 12:54











                      • @Hades I've picked the other answer as best because we still haven't learned for-each which you've used. The other answer is easy enough for me to understand what is happening also.

                        – Uponn
                        Nov 17 '18 at 12:33











                      • @Uponn but you explicitly mentioned not to use List , I havent , anyways whatever works for you.

                        – Hades
                        Nov 17 '18 at 12:42
















                      0














                      public static void main(String...strings) {
                      String para = "Paris in the the spring.Not that that is related.Why are you laughing? Are my my regular expressions THAT bad??";
                      String words = para.split("\s+");
                      int finalCount = 0;
                      int tempCount = 0;
                      String mostlyUsedWord = null;
                      for (String word: words) {
                      tempCount = 0;
                      for (String w: words) {
                      if (word.equalsIgnoreCase(w)) {
                      tempCount++;
                      }
                      }
                      if (tempCount >= finalCount) {
                      finalCount = tempCount;
                      mostlyUsedWord = word;
                      }
                      }
                      System.out.println("mostlyUsedWord:: = " + mostlyUsedWord + " ,count:: = " + finalCount);
                      }





                      share|improve this answer


























                      • This will gives "that" as the result, the search is case insensitive

                        – Hades
                        Nov 16 '18 at 12:47











                      • I think Pattern is a bit oversized here. Why not use an inner loop and String.equals?

                        – Ralf Renz
                        Nov 16 '18 at 12:51











                      • Thanks, it most likely words but as I said I have to use only for loops, if statements and some inbuild methods (indexOf, compare, isEqual and etc.). We still haven't learned about Pattern and Matcher so I have no idea what they do at the moment.

                        – Uponn
                        Nov 16 '18 at 12:54











                      • @Hades I've picked the other answer as best because we still haven't learned for-each which you've used. The other answer is easy enough for me to understand what is happening also.

                        – Uponn
                        Nov 17 '18 at 12:33











                      • @Uponn but you explicitly mentioned not to use List , I havent , anyways whatever works for you.

                        – Hades
                        Nov 17 '18 at 12:42














                      0












                      0








                      0







                      public static void main(String...strings) {
                      String para = "Paris in the the spring.Not that that is related.Why are you laughing? Are my my regular expressions THAT bad??";
                      String words = para.split("\s+");
                      int finalCount = 0;
                      int tempCount = 0;
                      String mostlyUsedWord = null;
                      for (String word: words) {
                      tempCount = 0;
                      for (String w: words) {
                      if (word.equalsIgnoreCase(w)) {
                      tempCount++;
                      }
                      }
                      if (tempCount >= finalCount) {
                      finalCount = tempCount;
                      mostlyUsedWord = word;
                      }
                      }
                      System.out.println("mostlyUsedWord:: = " + mostlyUsedWord + " ,count:: = " + finalCount);
                      }





                      share|improve this answer















                      public static void main(String...strings) {
                      String para = "Paris in the the spring.Not that that is related.Why are you laughing? Are my my regular expressions THAT bad??";
                      String words = para.split("\s+");
                      int finalCount = 0;
                      int tempCount = 0;
                      String mostlyUsedWord = null;
                      for (String word: words) {
                      tempCount = 0;
                      for (String w: words) {
                      if (word.equalsIgnoreCase(w)) {
                      tempCount++;
                      }
                      }
                      if (tempCount >= finalCount) {
                      finalCount = tempCount;
                      mostlyUsedWord = word;
                      }
                      }
                      System.out.println("mostlyUsedWord:: = " + mostlyUsedWord + " ,count:: = " + finalCount);
                      }






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Nov 17 '18 at 11:23

























                      answered Nov 16 '18 at 12:45









                      HadesHades

                      1,027825




                      1,027825













                      • This will gives "that" as the result, the search is case insensitive

                        – Hades
                        Nov 16 '18 at 12:47











                      • I think Pattern is a bit oversized here. Why not use an inner loop and String.equals?

                        – Ralf Renz
                        Nov 16 '18 at 12:51











                      • Thanks, it most likely words but as I said I have to use only for loops, if statements and some inbuild methods (indexOf, compare, isEqual and etc.). We still haven't learned about Pattern and Matcher so I have no idea what they do at the moment.

                        – Uponn
                        Nov 16 '18 at 12:54











                      • @Hades I've picked the other answer as best because we still haven't learned for-each which you've used. The other answer is easy enough for me to understand what is happening also.

                        – Uponn
                        Nov 17 '18 at 12:33











                      • @Uponn but you explicitly mentioned not to use List , I havent , anyways whatever works for you.

                        – Hades
                        Nov 17 '18 at 12:42



















                      • This will gives "that" as the result, the search is case insensitive

                        – Hades
                        Nov 16 '18 at 12:47











                      • I think Pattern is a bit oversized here. Why not use an inner loop and String.equals?

                        – Ralf Renz
                        Nov 16 '18 at 12:51











                      • Thanks, it most likely words but as I said I have to use only for loops, if statements and some inbuild methods (indexOf, compare, isEqual and etc.). We still haven't learned about Pattern and Matcher so I have no idea what they do at the moment.

                        – Uponn
                        Nov 16 '18 at 12:54











                      • @Hades I've picked the other answer as best because we still haven't learned for-each which you've used. The other answer is easy enough for me to understand what is happening also.

                        – Uponn
                        Nov 17 '18 at 12:33











                      • @Uponn but you explicitly mentioned not to use List , I havent , anyways whatever works for you.

                        – Hades
                        Nov 17 '18 at 12:42

















                      This will gives "that" as the result, the search is case insensitive

                      – Hades
                      Nov 16 '18 at 12:47





                      This will gives "that" as the result, the search is case insensitive

                      – Hades
                      Nov 16 '18 at 12:47













                      I think Pattern is a bit oversized here. Why not use an inner loop and String.equals?

                      – Ralf Renz
                      Nov 16 '18 at 12:51





                      I think Pattern is a bit oversized here. Why not use an inner loop and String.equals?

                      – Ralf Renz
                      Nov 16 '18 at 12:51













                      Thanks, it most likely words but as I said I have to use only for loops, if statements and some inbuild methods (indexOf, compare, isEqual and etc.). We still haven't learned about Pattern and Matcher so I have no idea what they do at the moment.

                      – Uponn
                      Nov 16 '18 at 12:54





                      Thanks, it most likely words but as I said I have to use only for loops, if statements and some inbuild methods (indexOf, compare, isEqual and etc.). We still haven't learned about Pattern and Matcher so I have no idea what they do at the moment.

                      – Uponn
                      Nov 16 '18 at 12:54













                      @Hades I've picked the other answer as best because we still haven't learned for-each which you've used. The other answer is easy enough for me to understand what is happening also.

                      – Uponn
                      Nov 17 '18 at 12:33





                      @Hades I've picked the other answer as best because we still haven't learned for-each which you've used. The other answer is easy enough for me to understand what is happening also.

                      – Uponn
                      Nov 17 '18 at 12:33













                      @Uponn but you explicitly mentioned not to use List , I havent , anyways whatever works for you.

                      – Hades
                      Nov 17 '18 at 12:42





                      @Uponn but you explicitly mentioned not to use List , I havent , anyways whatever works for you.

                      – Hades
                      Nov 17 '18 at 12:42



                      Popular posts from this blog

                      Bressuire

                      Vorschmack

                      Quarantine