Number of occurrences of each different word in a line of text I am looking for feedback on a solution to the following problem posed from a book that I'm working through (Java: How To Program 9th Edition):


  Write an application that reads a line of text and prints a table indicating the number of occurrences of each different word in the text. The application should include the words in the table in the same order in which they appear in the text. For example, the lines
  
  To be, or not to be: that is the question:
  Whether 'tis nobler in the mind to suffer
  
  contain the word “to” three times, the word “be” two times, the word “or” once, etc.


import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;

public class TextAnalysisC {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    Scanner sc = new Scanner( System.in );
    System.out.println( "Please enter a line of text" );
    String userInput = sc.nextLine();

    userInput = userInput.toLowerCase();

    userInput = userInput.replaceAll( "\\W", " " );     // strip out any non words.
    userInput = userInput.replaceAll( "  ", " " );      // strip out any double spaces
                                                        //   created from stripping out non words
                                                        //   in the first place!
    String[] tokens = userInput.split( " " );
    System.out.println( userInput );

    ArrayList&lt; String &gt; items = new ArrayList&lt; String &gt;();

    items.addAll( Arrays.asList( tokens ) );

    int count = 0;

    for( int i = 0; i &lt; items.size(); i++ )
    {
        System.out.printf( "%s: ", items.get( i ) );
        for( int j = 0; j &lt; items.size(); j++ )
        {
            if( items.get( i ).equals( items.get( j ) ) )
                count++;
            if( items.get( i ).equals( items.get( j ) ) &amp;&amp; count &gt; 1 )
                items.remove( j );                      // after having counted at least
        }                                               // one, remove duplicates from List        
        System.out.printf( "%d\n", count );
        count = 0;
    }
}
}


Can this be simplified?  Is this easy to understand?  What should I improve?

I regret to report that I have been remiss on the current scope of my knowledge as I work through my textbook (Java: 9th Edition). Solutions should be within the limits of what I have covered so far, that being:


Introduction to Computers and Java  
Introduction to Java Applications
Introduction to Classes, Objects, Methods and Strings
Control Statements: Part 1
Control Statements: Part 2
Methods: A Deeper Look
Arrays and ArrayLists
Classes and Objects: A Deeper Look
Object-Oriented Programming: Inheritance
Object-Oriented Programming: Polymorphism
Exception Handling: A Deeper Look
GUI Components: Part 1 (Swing)
Strings, Characters and Regular Expressions
The easiest way would be to use a LinkedHashMap&lt;String,AtomicInteger&gt;, adding the words in order.

LinkedHashMap is a Map, so duplicate keys can easily be detected. AtomicInteger will allow incrementing the value without having to replace the value in the Map. Yet AtomicInteger isn't exactly intended to be used as a dumb counter, making your own Counter class is also a good option, as long as it has an increment() and a get() method, it'll make your interaction with the Map a lot smoother.

You can use String.split() to break up the input String in words.

Quick pseudo code:

split input String into words
loop the words
   if word not in map
      add word as key, with new counter
   get counter for word and increment
loop map entries
   print word and number of occurrences


Edit

Given the scope of your knowledge, I'll make a suggestion that only uses what you should know already. The idea is to replace the Map by an ArrayList of words and a matching array of int to hold the counts.

split input String into words
initialize an int[] array with the size equal to the number of words // max needed capacity
initialize an ArrayList of Strings
loop the words
   determine index of the word in ArrayList.
   if word not in ArrayList
      add word to ArrayList
   increment int in array at matching index as the word in the ArrayList
loop ArrayList
   print word and number of occurrences (from same index in array)