Page 647 - Introduction to Programming with Java: A Problem Solving Approach
P. 647
In Figure 15.6a, the program starts by reading a user-specified filename into the filenameIn vari- able. Then it uses the entered filename to open the input file and create a Scanner object named fileIn to manage file reading operations.
The name of the output file should be the same as the name of the input file except for the extension, which should be .html. To compose the name of the output file, the String method lastIndexOf finds the index of the last dot in filenameIn. If there is no dot, the lastIndexOf method returns a value of -1, and the program simply appends .html to the original filename. If there is a dot, String’s substring method returns the part of the string up to the character immediately before the dot, and the program adds .html to that. This process replaces the original filename’s extension with a .html exten- sion, and it assigns the result to filenameOut.
Before examining the rest of the program, let’s digress by providing a brief overview of HTML (the computer language used to create Internet Web pages). This book is not about HTML, but the following overview will help you understand the HTMLGenerator program. Also, it’s worth learning a little about HTML because Internet Web pages are what got the Java language going.
HTML overview:
• HTML tags are surrounded by angled brackets, and they describe the purpose of their associated text.
• Tags without slashes (like <html>, <head>, and <title> tags) are called start tags. Tags with
slashes (like </title>, </head>, and </html> tags) are called end tags.
• <html> and </html> tags surround the entire Web page.
• Thecontentbetween<head>and</head>tagsistheheadingforanHTMLpage.Theheadingcon-
tains information that describes the HTML page. This information is used by the browser and by search
engines, but it is not vAisipbleaongthoe HTPMDL pFage. Enhancer
• <title> and </title> tags surround the text that appears in a Web page’s title bar. Internet search
engines use the <title> content to find Web pages.
• The content between <body> and </body> tags is the body for the HTML page. The body contains
the text that’s displayed on the HTML page.
• <h1> and </h1> tags surround the text that appears as a heading within a Web page. Web browsers
use large fonts to display text that’s surrounded by <h1> tags.
• <p> tags indicate the beginning of a new paragraph. Web browsers generate a blank line for each <p>
tag, and this helps set paragraphs apart.
Now let’s analyze Figure 15.6b, the second half of the HTMLGenerator program. The code starts by check- ing for an empty input file. If it’s empty, it prints a warning message. Otherwise, it does the following. It prints <html> and <head> tags to the output file. It prints the input file’s first line to the output file, sur- rounded by <title> and </title> tags. Then it ends the Web page’s head section by printing </head> to the output file, and it begins the Web page’s body section by printing <body> to the output file. Then it re-uses the input file’s first line and prints it to the output file, surrounded by <h1> and </h1> tags. Then it loops through the subsequent lines in the input file. For each blank line, it prints a <p> tag to the output file, indicating a new paragraph. It prints each line that’s not blank to the output file as is.
To see how the HTMLGenerator program works when applied to an actual input file, study the input file and resulting output file in Figure 15.7. If you’d like to verify that the HTMLGenerator program generates a working Web page, create Figure 15.7’s family.txt file, and then run the HTMLGenerator program with family.txt as input. That should generate Figure 15.7’s family.html file. Open a browser win- dow, and within that browser window, open the family.html file. For example, open a Microsoft Inter- net Explorer browser window and perform a File / Open command. Voila—you should see the family. html displayed as a Web page!
15.5 HTML File Generator 613