Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're cycling through each name in the list and checking it each time. Consequently, you're checking the name Jack 5 times, so you get 5 outputs.</p> <p>As for some reason you're not allowed to use HashMaps, this should solve it:</p> <pre><code>String name[] = new String[]{"jack", "jack", "jack", "james", "jack", "jack", "james"}; ArrayList&lt;String&gt; checkedNames = new ArrayList&lt;String&gt;(); for (int counter = 0; counter &lt; name.length; counter++) { String n = (name[counter]); if(!checkedNames.contains(n)) { int count = 0; for (int i = 0; i &lt; name.length; i++){ if (name[i].equals(n)) count++; } checkedNames.add(n); System.out.println(n + " - " + count); } } </code></pre> <p><strong>Edit</strong>: As you can't use ArrayLists but can sort the array:</p> <pre><code> String name[] = new String[]{"jack", "jack", "jack", "jack", "jack", "james"}; String lastName = ""; Arrays.sort(name); for (int counter = 0; counter &lt; name.length; counter++) { String n = (name[counter]); if(!lastName.equals(n)) { int count = 0; for (int i = 0; i &lt; name.length; i++){ if (name[i].equals(n)) count++; } System.out.println(n + " - " + count); } lastName = n; } </code></pre> <p><strong>Second edit</strong>:</p> <p>Firstly, Arrays is a class built into Java, just as String is:</p> <p><a href="http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html" rel="nofollow">http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html</a> <a href="http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html" rel="nofollow">http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/String.html</a></p> <p>Secondly, if you want it to print asterisks, then change the System.out.println line to this:</p> <pre><code>System.out.println(n + " (" + count + ")" + new String(new char[count]).replace("\0", "*")); </code></pre>
    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.
 

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