CSC 161 Lab 7-1: Files and Exceptions

Your Friend Jemima

Your friend Jemima Pistachio has a strange, and maybe a little bit nutty, desire to post excerpts from her electronic diary on the web. The dairy entries mention names of certain people that Jemima knows, or of celebrities she would like to meet someday. Jemima hopes that these people will read her dairy excerpts, and to make it easy for them to spot their names, Jemima wants each name to appear in a unique color. So far, the dairy entries mention the following names, listed with the colors that Jemima wants the respective names highlighted with.

Person Color
Rhonda Red
Betty Blue
Charles   Cyan  
Obama   Orange  
Gore   Green  
Peter   Purple  

Jemima has one other desire. She likes numbers, and she wants all numbers in her dairy excerpts to be highlighted using the color olive. Oh well, to each her own.

Example Input and Output

Here is an example of one of Jemima's diary excerpts.

 Dear dairy,

Last night I dreamed that I met the President of the United States of America.
In my dream, President Obama
 invited me to the WH to watch the super bowl. Former Vice President Albert 
Gore was there, and told me all I ever needed to know about Global Warming.
Then I woke up and went to see my friends Rhonda and Peter. While I was there,
Peter's friends Charles and Betty dropped by.  Charles and Betty got enganged yesterday,
and they will get married in June.

Here is an example of it should look when it is put on the web.

If you look carefully at the output, you will see that every name except Peter is highlighted in the appropriate color. This is because the program that produced the HTML has some shortcomings. The name Peter is immediately followed by a period the first time it appears, and it is followed by an apostrophe the second time. The punctuation fools the program and it fails to recognise the name.

Where You Come In

Being the kind-hearted person that you are, you have offered to write a Java program that can translate Jemima's dairy excerpts to HTML, so she can post them on the web. You need to pick up a few of HTML skills first.

Quick Introduction to HTML

A HTML element encloses some content between a start tag and an end tag. A simple html document is

Hello World of HTML!

Go to the Windows Start menu, select All Programs, then Accessories, and then select Notepad. Type in the above HTML code into Notepad, and then go to the File menu and selecte Save As... to save the the file in your Documents folder. Make sure you select All files(*.*) for the file type, and save the file with a .html extension. For example, you can use the file name Hello.html

Congratulations! you have just created an HTML file. This simple HTML document has a single html element.

A HTML file can be viewed through a browser. Start up your favourite browser and open the file that you have just created in the browser. In Internet Explorer and also in Firefox, you go to the File menu and then select Open. Navigate to your Documents folder and select the file Hello.html that you have just created. The browser will display the contents of your HTML file.

Now that you know how to create a simple HTML document and display it in a browser, let us learn the rest of the HTML skills that you need to help your friend Jemima.

A Little More HTML

Typically, different HTML elements define parts of a HTML document. A HTML element always has a start tag that looks like this

     <tag >

and a corresponding end tag that looks like this

     </tag >

Between the start tag and the end tag is the content of the element. In this case, the html start and end tags enclose the text

    Hello World of HTML!

as content.

Let us modify the HTML file we created so that it displays the word Hello in blue and the word HTML in pink. When displayed in a browser, the modified HTML file will look like this:

In HTML, you change the color of text by enclosing the text inside a <span > element, and then setting the style of the text in the span to the color you want. To achieve the effect in the screen shot above, open up the Hello.html file in Notepad and modify it to look like this:

     <span style="color:blue">Hello</span>World of <span style="color:pink">HTML</span>!

Now save it and view it in a browser as before.

How to Write the Program

Follow the these steps and hints to write the program for your friend Jemima.

1. Use a JFileChooser to open the input file and the output file. To open the input file use showOpenDialog(). To open the output file, use showSaveDialog(). Use two separate File objects to hold the selected files. You should first create a text file with the dairy excerpt content, name it dairy.txt and store it in your Documents folder. When you run the program and showSaveDialog asks for the name of a file to write to the first time, type in Dairy.html.

2. Use two arrays to establish an association between the names and their assigned colors. Then, use a map that associates the names and colors, as in the following code:

       String [] friends = {"Rhonda", "Betty", "Charles", "Obama",
                            "Gore", "Peter"};       
       String [] colors = {"Red", "Blue", "Cyan", "Orange", "Green", "Purple"};
       Map<String, String> colorMap = new HashMap<String, String>();

3. Put a Scanner object on the input File object so you can read it one word at a time. Words will be assumed to be separated from each other by white space.

4. Put a FileWriter object on the output File object so you can write to it. You can then use the write(String str) method of FileWriter to write to the ouput file.

5. The idea is that you will use the scanner to read the input file, one word at a time. Whenever you get a word, you look it up in the colorMap. If it is not there, you can go ahead and write the unadulterated word to the output file. But if you find the word in a map, you need to create an HTML span element that colors the word with color that is associated with the word in the colorMap. You can get the span element by calling the following method.

     * puts a word as the content of a HTML span element with a color style
     * @param word: the word to use as content of the span element.
     * @param color: the color to use in the style of the span element
     * @return the string comprising the constructed span element
    private static String colorize(String word, String color)
       StringBuilder sb = new StringBuilder("<span style=\"color:");
       return sb.toString();

6. Your program must use Exception handling. All checked exceptions must be caught and handled within the method in which they are thrown.

7. The example input above does not include numbers. Make sure you test your program with some input that include numbers and verify that it displays all numbers in the olive color.

Due Date: Friday night of Week 7.