Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: I'm trying to get the most frequent character out of a bunch

  1. #1
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default I'm trying to get the most frequent character out of a bunch

    My goal is to get the most frequent character referred to in the title by:

    1. Storing character-set in an array
    2. Rearranging the array - somehow - as Unicode-numbers
    3. Comparing the array-elements by having a loop notice the greatest number of times a character occurs (So they can't be outdone by the other-occurrences) to the point of getting the needed one back as a character
    4. Returning the most-frequent-character to the main-method


    Here is the method-code I'm using: (w/some extra variable-declarations, just in case.)
     
         // For telling the most frequent character in a String.
         public static char mostFrequent(String charToFind)
         {
              // Use this array(s) that tracks the highest-frequency-character.
              char wordArray[] = new char[16];
              char frequentFound[] = new char[16];
              char charsToCount,
                   freqChar;
     
     
              int iterator, 
                   mostFreqNum = 0,
                   frequentOne[] = new int[16],
                   buildUp = 0,
                   freqNum = 0,
                   highestFreq = 0,
                   loopCon = 16,
                   redoer = 1;
     
              int startSearch, index, iteration = 0, minValue, minIndex;
     
              wordArray = charToFind.toCharArray();
     
              for (int reiterate = 0; reiterate <= 15; reiterate++)
              {
                   frequentOne[reiterate] = (int) wordArray[reiterate];
              }  
     
              for (startSearch = 0; startSearch <= 15; startSearch++)
              {
                   minIndex = startSearch;
                   minValue = frequentOne[startSearch];
                   for (index = startSearch + 1; index < 15; index++)
                   {
                        if (frequentOne[index] < minValue)
                        {
                             minValue = frequentOne[index];
                             minIndex = index;
                        }
                   }
     
                   frequentOne[minIndex] = frequentOne[startSearch];
                   frequentOne[startSearch] = minValue;
              }
     
              while (15 >= 0)
              {
                   iteration++;
     
                   while (14 >= 0)
                   {
                        redoer++;
     
                        if (frequentOne[iteration] < frequentOne[redoer])
                        {
                             freqNum = frequentOne[redoer] - frequentOne[iteration];
                        }
     
                        if (frequentOne[iteration] > frequentOne[redoer])
                        {
                             freqNum = frequentOne[iteration] - frequentOne[redoer];
                        }
                   }
              }
     
              freqChar = (char) freqNum;
     
              return freqChar;
         }

    Now here is the compiler's current-complaint: It refers to the second-to-last statement as unreachable, so what is trying to reach it, and how can I correct this?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,247
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: I'm trying to get the most frequent character out of a bunch

    Can you make a small complete program that will show the error when compiled? I can't see the problem with the posted code.
    Also post the full text of any error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    1,069
    Thanks
    7
    Thanked 215 Times in 191 Posts

    Default Re: I'm trying to get the most frequent character out of a bunch

    while (15 >= 0)
    {
        // stuff happens here
    }
     
    freqChar = (char) freqNum;

    Fifteen is greater than or equal to zero and will remain so for all time. So the compiler complains that the assignment to freqChar will never be reached.

  4. #4
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: I'm trying to get the most frequent character out of a bunch

    I went ahead and redid my code to try something out, so I might as well give it to the forum as is - it's part of what I'm doing. Instead of using Unicode numbers, I just decided to use a matchup-algorithm. That, and plus, the frequent-character the method was supposed to give was 'a', not 'j'. I would appreciate any suggestions on the rearrangement-part of my code, along with suggestions on anything else, (the first for-loop) so here it is:

    public class Lettering
    {
         public static void main(String[] args)
         {
              char thisChar = charGiver("cdaahijcdaahijaa");
         }
     
         public static char charGiver(String charToFind)
         {
              // Use this array(s) that tracks the highest-frequency-character.
              char wordArray[] = new char[16];
     
              // Use the 2D-array to store a row for each of the 16 characters.
              char checkUp[][] = new char[16][16];
     
              // Initialize the most-frequent-character's variable.
              char freqChar = charToFind.charAt(0);
     
              // Initialize the number-variables that will hold the
              // length of each "row" inside themselves as the 
              // character-storage-loop goes along.
              int iterate,
                   freqNum = 0,
                   freqNum2 = 0;
     
              // Store all of the characters of the string in
              // a word-array.
              wordArray = charToFind.toCharArray();
     
              // Use this loop with it's nested-loop to rearrange the characters in another array.
              for (int reiterate = 0; reiterate <= 15; reiterate++)
              {
                   for (int indexer = 0; indexer <= 15; indexer++)
                   {
                        // Store each "row" of characters in the 2D-array.
                        if (charToFind.charAt(reiterate) == charToFind.charAt(indexer))
                        {
                             checkUp[reiterate][indexer] = wordArray[indexer];
     
                             System.out.println("Here is where the rearrangement happens: " + checkUp[reiterate][indexer]);
                        }
                   }
              }  
     
              // Get the character in the freqChar-variable using this loop.
              for (int reiterate = 0; reiterate <= 15; reiterate++)
              {
                   // This nested-loop checks each row and stores it's length in one of the 
                   // checkUp-array's element-dimensions.
                   for (iterate = 0; iterate <= 15; iterate++)
                   {
                        // Get the first-element-character representation in the row stored.
                        if (checkUp[reiterate][reiterate] == checkUp[reiterate][iterate] && iterate == 0)
                        {
                             freqNum2 = iterate;
                        }  
     
                        // Now get the rest of the character-representations in the row stored
                        // as far as length is concerned.
                        if (checkUp[reiterate][reiterate] == checkUp[reiterate][iterate] && iterate > 0)
                        {
                             freqNum = iterate;
                             freqNum2 = iterate;
                        }  
     
                        // Use both if-statements to store a character and it's replacement,
                        // and IT'S replacement to eventually store the right one in the end.
                        if (freqNum > freqNum2)
                        {
                             freqChar = checkUp[iterate][iterate];
                             System.out.println("Here is where the char-storage happens: " + freqChar);   
                        }
     
                        if (freqNum < freqNum2)
                        {
                             freqChar = checkUp[iterate][iterate];
                             System.out.println("Here is where the char-storage happens: " + freqChar);   
                        }
                   }   
              }  
     
              // Return the character when everything is finished.
              System.out.println(freqChar);
     
              return freqChar;
         }
    }

    Here is my output:
     

    Here is where the rearrangement happens: c
    Here is where the rearrangement happens: c
    Here is where the rearrangement happens: d
    Here is where the rearrangement happens: d
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: h
    Here is where the rearrangement happens: h
    Here is where the rearrangement happens: i
    Here is where the rearrangement happens: i
    Here is where the rearrangement happens: j
    Here is where the rearrangement happens: j
    Here is where the rearrangement happens: c
    Here is where the rearrangement happens: c
    Here is where the rearrangement happens: d
    Here is where the rearrangement happens: d
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: h
    Here is where the rearrangement happens: h
    Here is where the rearrangement happens: i
    Here is where the rearrangement happens: i
    Here is where the rearrangement happens: j
    Here is where the rearrangement happens: j
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the rearrangement happens: a
    Here is where the char-storage happens: c
    Here is where the char-storage happens: d
    Here is where the char-storage happens: a
    Here is where the char-storage happens: a
    Here is where the char-storage happens: h
    Here is where the char-storage happens: i
    Here is where the char-storage happens: j
    j



  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,247
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: I'm trying to get the most frequent character out of a bunch

    First problem is the use of magic/hardcoded values. The 16 and 15
    The length of the String passed to the method should be used vs hardcoding 16/15 in so many places.

    Can you explain the logic for the program? How does it count the occurences of each char in the String? What is the 2 dim array for?


    A suggestion to make the code easier and faster to debug: Use a shorter String.
    For example: "cbaa"
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: I'm trying to get the most frequent character out of a bunch

    I realize - now - that I must have gotten ahead of myself with the logic: I thought that using a 2D-array would help to arrange the characters as if they were in rows and columns. I tried to count the characters by using a for-loop with an internal-if-statement that asks the computer if one row-part matches with another, but found that more than 1 row-part would occur in the string to the point of the character-multiplication.

    I would also like to ask for an advisory tip (only if you want to give it): what would be more practical, telling the computer what to look for and where to put it beforehand, or just typing in the characters to the point of making the computer do all of the work?

  7. #7
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,550
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: I'm trying to get the most frequent character out of a bunch

    If you need to count the occurrences of chars in a String then what you need is an int array with a length of 26. Then iterate over the String one char at a time. If the char is an 'a' increment the int at index 0 of the array. If the char is 'z' increment the int at index 25 of the array.

    Hint:
    System.out.println((int)'a');
    There is no need to store any of the chars in a 2D array.
    Improving the world one idiot at a time!

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,247
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: I'm trying to get the most frequent character out of a bunch

    telling the computer what to look for and where to put it beforehand, or just typing in the characters to the point of making the computer do all of the work?
    Are you asking how to input the String to be searched? For testing I'd use a short String in the code.
    I have no idea what the 2D array is for. Seems too complicated for the simple task of finding the most frequent character.

    Do you know what is in the 2D array? Use the Arrays class's deepToString() method to format the array for printing. The contents of the 2D array will have to be initialized to ' ' for it to work. int 0s do not print well.

    I'm done for tonight. Back tomorrow.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: I'm trying to get the most frequent character out of a bunch

    What I was asking in the last question was if it was practical to get either me or the user to tell the computer what characters to look for beforehand, so making an array of characters to look for before a user uses the program does this, and telling the size of the array too, (which would seem to work without typing a number) or just to try to make the computer do all of the work with a random-set of characters with no warning from me/an array beforehand.

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,247
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: I'm trying to get the most frequent character out of a bunch

    Do it whichever way you want.

    What is the purpose of executing the program?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: I'm trying to get the most frequent character out of a bunch

    The purpose of executing the program is just for home-project practice with a textbook, but I wanted to see if I could get it done this way: without the warning for the computer. So, I need help with the syntax of the deepToString-method. How exactly is it typed?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,247
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: I'm trying to get the most frequent character out of a bunch

    Use the println() method to print what is returned by the deepToString() method. Something like:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  13. The Following User Says Thank You to Norm For This Useful Post:

    SOG (May 3rd, 2013)

  14. #13
    Member
    Join Date
    Jul 2011
    Posts
    62
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default Re: I'm trying to get the most frequent character out of a bunch

    I got the method to show the most frequent-character now. Thanks for your help.

    Norm: For testing I'd use a short String in the code.
    Now all I would have to do is do this sort of thing on a bigger scale (with a bigger loop-setup, but simplified) of any kind to do it again if it's needed.

Similar Threads

  1. [SOLVED] How do I save a bunch of images in a JPanel as one?
    By Yoyo_Guru in forum AWT / Java Swing
    Replies: 4
    Last Post: June 14th, 2012, 10:34 AM
  2. Frequent Interview qn
    By tcstcs in forum Java Theory & Questions
    Replies: 3
    Last Post: May 9th, 2012, 10:35 AM
  3. The character '' is an invalid XML character exception getting
    By sumanta in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 9th, 2010, 12:13 PM
  4. Hello ya bunch of programmers...
    By kaylors in forum Member Introductions
    Replies: 7
    Last Post: October 7th, 2009, 04:06 AM