title: List names and number of occurrences
body: <p>So the task was to read a file with the following names:</p>
  
  <pre><code>Alice
  Bob 
  James  
  Richard  
  Bob  
  Alice  
  Alice  
  Alice  
  James  
  Richard  
  Bob 
  Richard  
  Bob  
  Stephan  
  Michael  
  Henry   
  </code></pre>
  
  <p>And print out each name with its value of occurrence e.g "Alice - &lt;4>".
  <br/> I got it working, basically. The only problem I have is that the last name (Stephan - &lt;1>) is missing in my output and I can't get it to work properly.. It's probably because I used [i-1] but as I said, I'm not getting the right solution here. <br/>
  Well, here's my code..</p>
  
  <pre><code>package Assignment4;
  
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.io.BufferedReader;
  import java.util.Arrays;
  
  public class ReportUniqueNames {
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
  
          System.out.println ("       This programm counts words, characters and lines!\n");
          System.out.println ("Please enter the name of the .txt file:");
  
          BufferedReader input = new BufferedReader(new InputStreamReader (System.in));
  
          BufferedReader read = null;
          String file = "";
          String text = "";
          String line = "";
          boolean unique = true;
          int nameCounter = 1;        
  
          try {
  
              file = input.readLine();
              read = new BufferedReader (new FileReader(file));
              while ((line = read.readLine()) != null) {
                  text += line.trim() + " ";                  
              }
  
          } catch (FileNotFoundException e) {
              System.out.println("File was not found.");          
          } catch (IOException e) {
              System.out.println("An error has occured.");            
          }
  
          String textarray[] = text.split(" ");
          Arrays.sort(textarray);
  
          for (int i=0; i &lt; textarray.length; i++) {
  
              if (i &gt; 0 &amp;&amp; textarray[i].equals(textarray[i-1])) {
                  nameCounter++;
                  unique = false;
              }
  
              if (i &gt; 0 &amp;&amp; !textarray[i].equals(textarray[i-1]) &amp;&amp; !unique) {
                      System.out.println("&lt;"+textarray[i-1]+"&gt; - &lt;"+nameCounter+"&gt;");
                      nameCounter = 1;
                      unique = true;
              } else if (i &gt; 0 &amp;&amp; !textarray[i].equals(textarray[i-1]) &amp;&amp; unique) {
                  //nameCounter = 1;
                  System.out.println("&lt;"+textarray[i-1]+"&gt; - &lt;"+nameCounter+"&gt;");
              }           
  
          }
  
      }
  
  }
  </code></pre>
  
  <p>So that's it.. Hopefully one of you could help me out.</p>
  
  <p><strong>EDIT:</strong><br/> Wow, so many different approaches. <br/> First of all thanks for all of your help. <br/>I'll look through your suggested solutions and maybe restart from the bottom ;).<br/> Will give you a heads up when I'm done.</p>