Espresso: A Concentrated Introduction to Java

Laboratory: String Basics

Summary: In this laboratory, you will extend your knowledge of strings in Java.

Classes Used:

Corresponding Reading:



Exercise 0: Preparation

a. Review the reading on String Basics.

b. For this lab, you will call the project Strings and the package username.strings. Note that you should use your own username in place of username. You should create the directories

c. Start Eclipse and tell it to create a new project from that directory.

Exercise 1: Appending For Emphasis

Write and test a main class called Emphasize that prompts the user for a word, reads that word from the keyboard, builds a new string that surrounds the word with two stars and a space on each side, and prints out the result. Your result should look something like the following:

Please enter a word: Hello
** Hello **

Note that you may will probably need to use two calls to concat.

Exercise 2: Segmenting Strings

Programmers often find it convenient to store compound data in a text file with one line per entry. To separate the components of the entry, they use some designated symbol, such as a colon. For example, we might store student grades as


with sample entries of


Write a main class, Grader, that prompts the user for the name of a file, reads one line of the form above from the file, segments the line into the four parts (last name, first name, name of homework, and grade) and then prints the data in human-readable form, such as

John Doe received a 78 on HW1.

You may find it easiest to first ensure that you can print the last name, then to add the first name, and so on and so forth.

Note that you should use indexOf to find the index of the colon and then substring to extract the appropriate portion.

Exercise 3: De-leet-ing Text

You may be familiar with a juvenile form of writing, known as 133+ or leet, in which alternate characters or sequences of characters are used in place of familiar alphabetics. For example, a plus sign (+) is used in place of the letter t, a 3 in place of the letter e, the numeral 1 in place of the letter l, and the numeral 0 in place of the letter o. In some cases, multiple symbols are used in place of a single letter, such as a vertical bar and a 3 in place of b or B. or a vertical bar, a backslash, and a vertical bar in place of n.

Write a main class, DeLeet, which prompts the user for a phrase in this odd language and attempts to return that phrase to its English form. Here is a sample run of the program

Whassup? 3@+ |3@|\|@|\|@
I think you said 'eat banana'.

Exercise 4: De-leet-ing Text, Revisited

If you used a String in the last exercise to store the phrase, try redoing the exercise with a StringBuffer.

Similarly, if you used a StringBuffer in the last exercise to store the phrase, try redoing the exercise with a String.

Which, if either, do you prefer? Why?

Extra Work

If you finish the lab early, you may want to try some or all of the following.

Extra 1: The Name Game

You may be familiar with Shirley's Ellis's Name Game. Ms. Ellis describes her procedure for developing phrases based on her colleague's names as follows:

Come on everybody!
I say now let's play a game
I betcha I can make a rhyme out of anybody's name
The first letter of the name, I treat it like it wasn't there
But a B or an F or an M will appear
And then I say bo add a B then I say the name and Bonana fanna and a fo
And then I say the name again with an F very plain
and a fee fy and a mo
And then I say the name again with an M this time
and there isn't any name that I can't rhyme.

She also gives us a number of examples:

Shirley, Shirley bo Birley Bonana fanna fo Firley
Fee fy mo Mirley, Shirley!

Lincoln, Lincoln bo Bincoln Bonana fanna fo Fincoln
Fee fy mo Mincoln, Lincoln!

Write a main class, NameGame, that gets a name from the user and prints out a verse of the form Ms. Ellis suggests. Your program need not handle names that begin with vowels or with more than one consonant.

Extra 2: A Simple Editor

Write a program that prompts the user for a filename, an incorrectly spelled word, and the correct spelling of that word. It should then read the first three lines from the file, correct that misspelled word, and write the revised lines to a new file with a generated name. For example, if the original file name was "hw1", the result should be in "corrected-hw1".

You might also try writing the corrected text to the original file.


Wednesday, 2 February 2005 [Samuel A. Rebelsky]

Friday, 4 February 2005 [Samuel A. Rebelsky]

Wednesday, 7 September 2005 [Samuel A. Rebelsky]

Wednesday, 1 February 2006 [Samuel A. Rebelsky]

This page was generated by Siteweaver on Thu Mar 30 15:24:43 2006.
The source to the page was last modified on Wed Feb 1 08:52:15 2006.
This page may be found at

You may wish to validate this page's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky