AP Java Assignment 6
Parsing Text Input

Background: Parsing

Parsing means to "To analyze or separate (input, for example) into more easily processed components." In this assignment, you will count the number of words in a TextArea by counting the number of spaces. Your applet will traverse the input, one character at a time, looking for characters that are not letters. It will display each word along with a word count every time the user presses the "parse next word" button.

Steps to completing this assignment

  1. Start with an applet that displays a TextArea and a Button. Look at the description of TextAreas below. You only need to add an ActionListener for the Button, not for the TextArea.
  2. You will need the following import statements:
    
    import java.awt.*;
    import java.applet.*;
    import java.awt.event.*;
  3. In actionPerformed you want to "lock" the TextArea up once the Button is pressed. You do this by calling the TextArea member method setEditable(false).
  4. Also in actionPerformed you will need a local String variable. Store the text in the TextArea in the local String variable by calling the getText() member method of the TextArea class
  5. Define two member variables of the applet class:
    
    int nFirstLetter;
    int nLastLetter;
    initialize nLastLetter to -1.
  6. To find the first and last letters of a word, we will implement the following algorithm in actionPerformed:
    1. Set nFirstLetter to nLastLetter + 1
    2. While the character at position nFirstLetter is NOT a letter of the alphabet, increase nFirstLetter by 1. See the example of isLetter() below.
    3. Set nLastLetter equal to nFirstLetter
    4. While nLastLetter is less than the length of the String AND the character at position nLastLetter IS a letter of the alphabet, increase nLastLetter by 1.
    5. nFirstLetter should now hold the position of the first letter of a word and nLastLetter should hold the position of the last letter. Call the select() member method of the TextArea class with two arguments: nFirstLetter and nLastLetter. This should highlight the appropriate word.
  7. Add a member variable to the Applet class called nWordCount. Initialize it to zero. Use this variable to count the words and display the current word count to the screen.

TextArea

For this assignment, you will use a TextArea to get input. A TextArea works similarly to a TextField except you can specify two arguments in the constructor, the first for number of rows and the second for number of coloumns:

TextArea entry = new TextArea(10,30);
You can also specify that the scroll bars are vertical or horizontal only:
TextArea entry = new TextArea("",10,30,TextArea.SCROLLBARS_VERTICAL_ONLY);

Other features that you may find useful include select() and setEditable(). You can find out more in the Java API.

isLetter() & substring()

You may find isLetter() from the Character wrapper class helpful. The following code would display false:

System.out.println(Character.isLetter('@'));
You may also find the substring() member method of the String class helpful. Here's an example:

String sString  = new String("Hello, how are you?");
System.out.println(sString.substring(15,19));
//displays you?

The Java API

The Java API (Applications Programming Interface) is like a giant help file. It lists all the Java classes, with their member methods and variabes. It's available online at from Sun, the developers of Java. (Programmers like to joke that API stands for "all poo inside". Programmers don't get out much.)

You will need a web page to display your applet. Your homepage should have a link to the web page for this assignment.

Submit the URL of your applet with the attached Java code (the .java file(s)) in an email message to mrsimon@lycos.com.