Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

You will finish a program to count the number of lines, tokens, characters, and bytes in a file. You will need to handle exceptions as

You will finish a program to count the number of lines, tokens, characters, and bytes in a file. You will need to handle exceptions as well.

Task

Your task to modify the code in lab8.zip so that the user can select a file, the program counts the number of words, tokens, characters, and bytes in the file, and this information is printed.

Lab8.java contains a main method and a getFileFromUser method. The getFileFromUser method gets a file name from the user using JFileChooser. THis method will throw a FileNotFoundException in some cases.

The main method of Lab8.java contains code with some problems. One problem is that no exceptions are handled. Another problem is that the code should continue to run until a file is successfully processed.

The FileCounts class is set up to store a File object. Each of the methods lineCount, tokenCount, charCount, and byteCount should:

Open the file using the appropriate java.io class.

Count the number of lines/tokens/chars/bytes and detect end of file using the appropriate methods of the class.

Close the file and return the count.

For counting lines, use the Scanner class and its hasNextLine and nextLine methods.

For counting tokens (words), use the Scanner class and its hasNext and next methods.

For counting characters, use the FileReader class and its read method. Read the documentation for how you detect the end of file using the read method. To be more efficient in reading large files, you should also use the BufferedReader class.

For counting bytes, use the FileInputStream class and its read method. To be more efficient in reading large files, you should also use the BufferedInputStream class.

In all of the above, you will need to add code to handle exceptions. The methods in FileCounts should pass any exceptions to the main method. The main method should keep trying to print out the information in a file selected by the user until no exception occurs.

Lab8.zip-

import java.io.*;

import javax.swing.*;

public class Lab8 {

/**

* Main method for Lab 8.

*/

public static void main(String[] args) {

// These statements should be be into a loop that runs until all the

// statements execute without an exception.

File file = getFileFromUser();

FileCounts counter = new FileCounts(file);

System.out.println(file);

System.out.printf("%d lines ", counter.lineCount());

System.out.printf("%d tokens ", counter.tokenCount());

System.out.printf("%d characters ", counter.charCount());

System.out.printf("%d bytes ", counter.byteCount());

}

/**

* Asks the user for a file name using the GUI.

*

* This is a modification of getFileOrDirectory from FileChooserDemo so it

* throws exceptions rather than exits when a problem is encountered. It

* also starts the GUI at the current directory.

*

* @return the file selected by the user

* @throws FileNotFoundException

*/

private static File getFileFromUser() throws FileNotFoundException {

// display file dialog, so user can choose file or directory to open

JFileChooser fileChooser = new JFileChooser(".");

fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

int result = fileChooser.showOpenDialog(null);

// if user clicked Cancel button on dialog, throw exception

if (result == JFileChooser.CANCEL_OPTION)

throw new FileNotFoundException("User selected cancel");

File fileName = fileChooser.getSelectedFile(); // get File

// display error and throw exception if invalid

if ((fileName == null) || (fileName.getName().equals(""))) {

JOptionPane.showMessageDialog(null, "Invalid Name", "Invalid Name",

JOptionPane.ERROR_MESSAGE);

throw new FileNotFoundException("Invalid Name: " + fileName);

} // end if

return fileName;

} // end method getFileFromUser

}

import java.io.*;

public class FileCounts {

private File file;

public FileCounts(File file) {

this.file = file;

}

public int lineCount() {

return -1;

}

public int tokenCount() {

return -1;

}

public int charCount() {

return -1;

}

public int byteCount() {

return -1;

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Structured Search For Big Data From Keywords To Key-objects

Authors: Mikhail Gilula

1st Edition

012804652X, 9780128046524

More Books

Students also viewed these Databases questions

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago