Write a program(java) that reads words from a text file and displays all the non
ID: 3752487 • Letter: W
Question
Write a program(java) that reads words from a text file and displays all the nonduplicae word in ascending order. The Text file is passsed as a command line argument.
parse the text file by splitting on "[ .,;:!?(){}<>"]".
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.
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
// PrintWords.java
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class PrintWords {
public static void main(String[] args) throws FileNotFoundException {
// making sure file name is passed as an argument
if (args.length == 0) {
System.out.println("Please pass file name as argument!");
System.exit(0);
}
// Creating an array list to store words
ArrayList<String> words = new ArrayList<String>();
// defining a file object using argument
File file = new File(args[0]);
// Creating a scanner object.
Scanner scanner = new Scanner(file);
// using the provided regex pattern to split Strings
scanner.useDelimiter("[ .,;:!?(){}<>"]");
// looping until all words have been read
while (scanner.hasNext()) {
// getting next word, converting to lower case
String word = scanner.next().toLowerCase();
// adding to the array list, if not already exist
if (!words.contains(word)) {
words.add(word);
}
}
// sorting the list of words using built in method, in ascending order
Collections.sort(words);
System.out.println("Found " + words.size() + " non duplicate words: ");
// displaying the words
System.out.println(words);
/**
* This is one way of handling this problem. Using array list is more
* practical here as we don't know the number of words before opening a
* file, and it's comparatively faster to check if a word exists in the
* list., Also easier to sort by ascending order of the words. Another
* two ways of implementing this would be using a common array(less
* efficient as it is not dynamic, need costly traversal to check if
* there is a word already added to an array), and using a Set [HashSet]
* (Faster and more efficient than array list in adding words as set
* will not have duplicate values, but difficult to sort the set)
*/
}
}
//Input.txt
I am happy to take your donation; any amount will be greatly appreciated.
I will never be this young again. Ever. Oh dam I just got older.
This is a Japanese doll.
I was very proud of my nickname throughout high school but today- I couldn’t be any different to what my nickname was.
Someone I know recently combined Maple Syrup & buttered Popcorn thinking it would taste like caramel popcorn. It didn’t and they don’t recommend anyone else do it either.
A glittering gem is not enough.
/*OUTPUT*/
Found 70 non duplicate words:
[, &, a, again, am, amount, and, any, anyone, appreciated, be, but, buttered, caramel, combined, couldn’t, dam, didn’t, different, do, doll, donation, don’t, either, else, enough, ever, gem, glittering, got, greatly, happy, high, i, is, it, japanese, just, know, like, maple, my, never, nickname, not, of, oh, older, popcorn, proud, recently, recommend, school, someone, syrup, take, taste, they, thinking, this, throughout, to, today-, very, was, what, will, would, young, your]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.