Question

Class: Java

Book: Introduction To Java Programming 10th Edition, Comprehensive by Daniel Liang

Problem:

****** Parse the text file by splitting on "[ \n\t\r.,;:!?(){}<>\"]", that is call the split method with this parameter: For example, call contents.split("[ \n\t\r.,;:!?(){}<>\"]"), where contents is a String object containing the contents of the text file.

Also, explain (in the comment block at the beginning of the source file) two other approaches that could have been taken for the problem (use of other data structures, perhaps?) and why you chose the approach you took as opposed to the alternatives. Compare and contrast your approach relative to these other approaches in terms of coding complexity, performance, and memory usage.​ ******

21.2 (Display nonduplicate words in ascending order) Write a program that reads words from a text file and displays all the nonduplicate words in ascending order The text file is passed as a command-line argument.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.util.*;
import java.io.*;

public class Exercise_21_02 {
    public static void main(String[] args) throws Exception {
        // Check length of command-line argument
        if (args.length != 1) {
            System.out.println("Usage: java fileName");
            System.exit(1);
        }

        // Check if file exists
        File textFile = new File(args[0]);
        if (!textFile.exists()) {
            System.out.println("The file " + args[0] + " does not exist.");
            System.exit(2);
        }

        // Create a set
        TreeSet<String> set = new TreeSet<>();

        // Read nonduplicate words from the file
        try ( // Create an input file
              Scanner input = new Scanner(textFile);
        ) {
            while (input.hasNext()) {
                String[] words = input.nextLine().split("[ \n\t\r.,;:!?()-]");
                for (String word: words) {
                    if (word.length() > 0)
                        set.add(word.toLowerCase());
                }
            }
        }

        // Display nonduplicate word in ascending order
        System.out.println(set);
    }
}

Add a comment
Know the answer?
Add Answer to:
Class: Java Book: Introduction To Java Programming 10th Edition, Comprehensive by Daniel Liang Problem: ****** Parse...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Chapter 7 Exercise 18, Introduction to Java Programming, Tenth Edition Y. Daniel Liang. Please write your...

    Chapter 7 Exercise 18, Introduction to Java Programming, Tenth Edition Y. Daniel Liang. Please write your own code. 7.18 (Bubble sort) Write a sort method that uses the bubble-sort algorithm. The bubblesort algorithm makes several passes through the array. On each pass, successive neighboring pairs are compared. If a pair is not in order, its values are swapped; otherwise, the values remain unchanged. The technique is called a bubble sort or sinking sort because the smaller values gradually “bubble” their...

  • CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the fil...

    CSC110 Lab 6 (ALL CODING IN JAVA) Problem: A text file contains a paragraph. You are to read the contents of the file, store the UNIQUEwords and count the occurrences of each unique word. When the file is completely read, write the words and the number of occurrences to a text file. The output should be the words in ALPHABETICAL order along with the number of times they occur and the number of syllables. Then write the following statistics to...

  • You need not run Python programs on a computer in solving the following problems. Place your...

    You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...

  • Hi there! I need to compare two essay into 1 essay, and make it interesting and...

    Hi there! I need to compare two essay into 1 essay, and make it interesting and choose couple topics which im going to talk about in my essay FIRST ESSAY “Teaching New Worlds/New Words” bell hooks Like desire, language disrupts, refuses to be contained within boundaries. It speaks itself against our will, in words and thoughts that intrude, even violate the most private spaces of mind and body. It was in my first year of college that I read Adrienne...

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
ADVERTISEMENT