Write a program that asks a user for a file name and prints the number of characters, words, and lines in that file.
the FileCounter class.
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFileChooser;
/**
This class prints a report on the contents of a file.
*/
public class FileAnalyzer
{
public static void main(String[] args) throws IOException
{
JFileChooser chooser = new JFileChooser();
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION)
{
FileReader reader = new FileReader(chooser.getSelectedFile());
Scanner fileIn = new Scanner(reader);
FileCounter counter = new FileCounter();
counter.read(fileIn);
fileIn.close();
System.out.println("Characters: " + counter.getCharacterCount());
System.out.println("Words: " + counter.getWordCount());
System.out.println("Lines : " + counter.getLineCount());
}
}
}
import java.util.Scanner;
/**
A class to count the number of characters, words, and lines in files.
*/
public class FileCounter
{
/**
* instance variables
*/
//your code here...
/**
Constructs a FileCounter object.
*/
public FileCounter()
{
//Your code here . . .
}
/**
Processes an input source and adds its character, word, and line
counts to this counter.
@param in the scanner to process
*/
public void read(Scanner in)
{
while (in.hasNextLine())
{
String line=in.nextLine();
//Your code here. . .
}
}
/**
Gets the number of words in this counter.
@return the number of words
*/
public int getWordCount()
{
//Your code here. . .
}
/**
Gets the number of lines in this counter.
@return the number of lines
*/
public int getLineCount()
{
//Your code here . . .
}
/**
Gets the number of characters in this counter.
@return the number of characters
*/
public int getCharacterCount()
{
//Your code here . . .
}
}