CS 239 Advanced Programming
Lab239-03: Parsing a String

Objectives

The students will be able to:

Background     

If you look at a word processing application such as Word, it has the ability to count various features of the document, such as number of words and number of characters. This application will let you explore how they might have done that. Bring a copy of your program to class tomorrow in either hardcopy form or electronically.

New Terms

token
A unit from some text.
delimiter
A character (or group of characters) which are used to separate tokens in text.
parse
The process of separating text into tokens
String Tokenizer
A Java class that has tools to help you "parse" a String into "tokens"

Materials Used

This application will be written from scratch.

 


Part 1 - The application

Write a program that prompts the user for a phrase. Using methods from the String and StringTokenizer classes, report back:

  1. the number of words in the phrase,
  2. the number of characters (not including spaces) in the phrase,
  3. the number of vowels in the phrase,
  4. the number of consonants in the phrase, and
  5. the number of punctuation marks.

Within your code, you should:

  1. use the Scanner class to handle user input,
  2. use an appropriate looping mechanism to parse the phrase,
  3. use the charAt() method of the String class to position the loop pointer, and
  4. use the StringTokenizer class to parse the phrase.

Your "main" method should be relatively bare. I would suggest that, to save time when testing, it include a loop to repeatedly ask the user for input.

From here, you should call other methods in (an)other class(es) to handle the tasks to be done. 

A brief overview of the StringTokenizer class is available here A short program showing some of the methods of the class is available here .

When you are finished, bundle your file(s) into a zip file.  You can do this from the command line in Linux in the form: 

   zip < name of zip file > [List of files to include - wildcards can be used here]

The name of the zip file should combine the lab number and the last names of each member of the pair (or a single last name if solo).  For example:

   zip Lab3NortonHarris.zip *.java

This will create a file called "Lab3NortonHarris.zip" that includes all file(s) that end with .java in the current directory).  Submit the zip file to Blackboard BEFORE the beginning of class on Monday.


created by Michael Norton
last updated 01/12/13 - nlh