Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>There's no need to make a <code>HashTable/HashMap/HashSet</code> for this.</em></p> <p><strong>You know which characters are being tracked ahead of time, so you can use an array.</strong></p> <blockquote> <p>I want to count the occurrence of all letters and numbers</p> </blockquote> <p>Make a string of the characters you will track, then initialize an array.</p> <pre><code>String sentence = "ABC ABC ABC 123"; //Make a map of all the characters you want to track. String indexes = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //Initialize an array to the size of the possible matches. int[] count = new int[indexes.length()]; //Loop through the sentence looking for matches. for (int i = 0; i &lt; sentence.length(); i++) { //This will get the index in the array, if it's a character we are tracking int index = indexes.indexOf(sentence.charAt(i)); //If it's not a character we are tracking, indexOf returns -1, so skip those. if (index &lt; 0) continue; count[index]++; } </code></pre> <p>Then you can print them all out with this:</p> <pre><code>for (int i = 0; i &lt; count.length; i++) { if (count[i] &lt; 1) continue; System.out.println(String.format("%s (%d) %s", indexes.charAt(i), count[i], //This little bit of magic creates a string of nul bytes, then replaces it with asterisks. new String(new char[count[i]]).replace('\0', '*'))); } </code></pre> <p>If you aren't comfortable with the <code>new String(new char[count[i]]).replace('\0', '*'))</code> bit, then you can use a <code>StringBuilder</code> to build the asterisk <code>String</code> before trying to output it. You can see <strong><em>@mike</em></strong>'s example below for a good example of that.</p> <h3>Outputs</h3> <pre><code>1 (1) * 2 (1) * 3 (1) * A (3) *** B (3) *** C (3) *** </code></pre> <h3>Considerations</h3> <p>Here are some things to consider when decided how to solve this problem.</p> <ul> <li>Will you always know what characters need to be tracked ahead of time, or will there be times when you want to track <em>any</em> character? In the latter case, an array won't work for you; you would need to use an advanced data structure like a TreeMap or HashMap.</li> <li>Will you always be counting occurrences of specific <code>char</code>s, as opposed to <code>String</code>s? If you have to modify this to count <code>String</code>s then using the <code>String indexes</code> map trick isn't going to work for you either.</li> <li>Are you learning a specific data structure(s) in your course at the moment? Usually problems such as this are assigned to students to understand how to apply a specific concept. As <strong><em>@kyle</em></strong> suggested, you should try to use the data structure that you are learning about, or have learned about, in your class. <em>Sometimes using structures that you haven't learned about yet can get you in trouble, or a lower grade at least.</em></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload