Question

Write a Java program that reads words from a text file and displays all the non-duplicate...

Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument.

Command line argument: test2001.txt

Correct output:

Words in ascending order...

1mango

Salami

apple

banana

boat

zebra

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;

public class WordsInFile {

    public static void main(String[] args) {
        if (args.length > 0) {
            File file = new File(args[0]);
            try {
                Scanner fin = new Scanner(file);
                Set<String> words = new TreeSet<>();
                while (fin.hasNext()) {
                    words.add(fin.next());
                }
                System.out.println("Words in ascending order...");
                for (String word : words) {
                    System.out.println(word);
                }
                fin.close();
            } catch (FileNotFoundException e) {
                System.out.println(file.getAbsolutePath() + " is not found!");
            }
        } else {
            System.out.println("Please provide filename as command line argument");
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Write a Java program that reads words from a text file and displays all the non-duplicate...
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
  • Please help me with this question. It is in java and if possible please provide details....

    Please help me with this question. It is in java and if possible please provide details. Thank you so much. With this question there is no file provide, just make one up. Write a program that reads words from a text file and displays all the words(duplicates allowed) in ascending alphabetical order. The words must start with a letter. The text file is passed as a command-line argument.

  • out1.txt File Directly Below... Note: "//notaword" is the part that is not a word and needs...

    out1.txt File Directly Below... Note: "//notaword" is the part that is not a word and needs to be handled and is specifically the part I am having trouble with https://www.dropbox.com/s/ume3slnphyhi6wt/out1.txt?dl=0 - link for the out1.txt file for testing yourself Please include comments as I am in this for learning and would really appreciate the help! Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start...

  • Write a java program that reads a file (names as textfile) uploaded with assignment and displays...

    Write a java program that reads a file (names as textfile) uploaded with assignment and displays the words of that file as a list. Then display the words in reverse order. Then display them with all plurals (ending in "s") capitalized. Then display them with all plural words removed.

  • 12.13 (Count characters, words, and lines in a file) Write a program that will count the...

    12.13 (Count characters, words, and lines in a file) Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown in Figure 12.13 D Command Prompt exercise java Exercise12.13 Loan.java ile Loan.jaua has 1919 characters 10 words 71 lines lexercise Figure 12.13 The program displays the number of characters, words, and lines in the given file. This...

  • Class: Java Book: Introduction To Java Programming 10th Edition, Comprehensive by Daniel Liang Problem: ****** Parse...

    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,...

  • In C write a text analyzer program that reads any text file. The program displays a...

    In C write a text analyzer program that reads any text file. The program displays a menu that gives the user the options of counting: • Lines • Words • Characters • Sentences (one or more word ending with a period) • or all of the above Provide a separate function for each option. At the end of the analysis, write an appropriate report.

  • (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text...

    (Count the occurrences of words in a text file) Rewrite Listing 21.9 to read the text from a text file. The text file is passed as a command-line argument. Words are delimited by white space characters, punctuation marks (, ; . : ?), quotation marks (' "), and parentheses. Count the words in a case-sensitive fashion (e.g., consider Good and good to be the same word). The words must start with a letter. Display the output of words in alphabetical...

  • Write a program that reads the scores from the file and displays their total and average.

    Suppose that a text file Lab2.txt contains an unspecified number of scores. Write a program that reads the scores from the file and displays their total and average.In the text file, the scores are separated by blanks.The program should contain two methods with these signatures (it can contain other methods besides these if you think appropriate):public static double totalScores(File inFile)public static double averageScores(File inFile)The output from the program should look like this successful sample run from my solution:The total of...

  • JAVA: create a program that reads all text files in a directory, calculates the occurrences of...

    JAVA: create a program that reads all text files in a directory, calculates the occurrences of words, and saves the result in one single output text file. assume that you are in a directory called Desktop/programdirectory, where you have multiple text files. textfile1.txt : "how are you" textfile2.txt: "great thank you" textfile3.txt: "great to see you. see you later" Then, your output.txt should have: how: 1 are: 1 you: 4 great: 2 thank: 1 to: 1 see: 2 later: 1

  • Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list...

    Write a Java program called EqualSubsets that reads a text file, in.txt, that contains a list of positive and negative integers (duplicates are possible) separated by spaces and/or line breaks. Zero may be included. After reading the integers, the program saves them in a singly linked list in the same order in which they appear in the input file. Then, without changing the linked list, the program should print whether there exists two subsets of the list whose sums are...

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
ADVERTISEMENT