HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Number of occurrences of each different word in a line of text

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numbereachoccurrenceslinetextdifferentword

Problem

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 items = new ArrayList();

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

    int count = 0;

    for( int i = 0; i  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

Solution

The easiest way would be to use a LinkedHashMap`, 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)

Code Snippets

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
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)

Context

StackExchange Code Review Q#29091, answer score: 2

Revisions (0)

No revisions yet.