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< String > items = new ArrayList< String >();
items.addAll( Arrays.asList( tokens ) );
int count = 0;
for( int i = 0; i < items.size(); i++ )
{
System.out.printf( "%s: ", items.get( i ) );
for( int j = 0; j < items.size(); j++ )
{
if( items.get( i ).equals( items.get( j ) ) )
count++;
if( items.get( i ).equals( items.get( j ) ) && count > 1 )
items.remove( j ); // after having counted at least
} // one, remove duplicates from List
System.out.printf( "%d\n", count );
count = 0;
}
}
}
I'm interested in all forms of feedback, can this be simplified? Is this easy to understand? What should I improve?
Edit(30/7/2013) - 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
a b b b c
as input and see the result. – tintinmj Jul 28 '13 at 18:52items.remove( j )
reordering the sequence of letters in the ArrayList. I think it could be confusing things, I'll try and fix it in the morning. A better algorithm might be in order. – cryptocurry Jul 28 '13 at 19:39