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.
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.
import statements:
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
actionPerformed you want to "lock" the TextArea up once the Button is pressed. You do this
by calling the TextArea member method setEditable(false).
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
int nFirstLetter;
int nLastLetter;
initialize nLastLetter to -1.
actionPerformed:
nFirstLetter to nLastLetter + 1
nFirstLetter is NOT a letter of the alphabet, increase nFirstLetter
by 1. See the example of isLetter() below.
nLastLetter equal to nFirstLetter
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.
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.
Applet class called nWordCount. Initialize it to zero. Use this
variable to count the words and display the current word count to the screen.
TextAreaTextArea 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()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?
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.