Problem

The program below is supposed to open a file whose full pathname is provided by the user i...

The program below is supposed to open a file whose full pathname is provided by the user in a keyboard entry. Then it is supposed to count the number of words in the file, where any kind of white space is a word delimiter. The program is complete except for a code fragment of several lines in the try block. Provide the missing code fragment. Use an anonymous File object as the Scanner argument when you instantiate the fileIn object. Of course, you can use the same Scanner-class hasNext and next 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){Scanner stdIn = new Scanner(System.in);Scanner fileIn;int numWords = 0;try{<fragment>} // end trycatch (FileNotFoundException e){System.out.println("Invalid filename.");}catch (Exception e){System.out.println("Error reading from the file.");}} // end main} // end WordsInFile class

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.txtNumber of words = 63

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 15
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT