Page 672 - Introduction to Programming with Java: A Problem Solving Approach
P. 672
638
Chapter 15 Files
methods for the fileIn object that you used before with the stdIn object when reading from the keyboard.
/*************************************************************
*
WordsInFile.java
Dean & Dean
This counts the words in a text file.
*************************************************************/
*
*
*
import java.io.*;
import java.util.*;
public class WordsInFile
{
public static void main(String[] args)
{
}
// end main
Scanner stdIn = new Scanner(System.in);
Scanner fileIn;
int numWords = 0;
try
{
<fragment>
} // end try
{
}
}
Apago PDF Enhancer
System.out.println("Invalid filename.");
catch (FileNotFoundException e)
catch (Exception e)
{
System.out.println("Error reading from the file.");
6.
If the file is the family.txt file displayed in the following exercise, you should get something like this:
Sample session:
Enter full pathname of file:
e:/myJava/problems/chapter15/family.txt
Number of words = 63
[after §15.5] As explained in the text, strict HTML standards require all p start tags (<p>) to have an accompanying p end tag (</p>). Edit the HTMLGenerator.java program given in the text so that p end tags (</p>) are inserted properly in the generated HTML file. The p end tags should be inserted at the bottom of each paragraph.
Note:
• In the family.txt file below, assume that there is a newline character at the end of each line.
• Donotallowapendtagtobegeneratedwhenthere’snoaccompanyingpstarttag(startandendtags
}
// end WordsInFile class
must always be partnered).